So the thing is I have a class which has a foreign key.
This is my code
class Proxy(models.Model):
class Meta:
db_table = 'Proxy'
equipment = models.ForeignKey('Equipment', primary_key=True)
pop = models.ForeignKey('Pop')
Now, as usual when I do
import django.core.serializers as Serializer
res = Proxy.objects.filter(equipment_id__exact='eq1')
Serializer.serialize('json', res)
the json output contains the "id" of the Pop and not the name, which I want.
So I used the Manager class and this is my Pop class now-
class PopManager(models.Manager):
def get_by_natural_key(self, name):
return self.get(name=name)
class Pop(models.Model):
POP_TYPES = (
('phy','phy'),
('cloud','cloud'),
)
class Meta:
db_table = 'Pop'
unique_together = ('name', 'vip')
objects = PopManager()
name = models.CharField(max_length=10)
type = models.CharField(max_length=10, choices=POP_TYPES)
def natural_key(self):
return (self.name)
But after this, when I do
res = Proxy.objects.filter(equipment_id__exact='eq1')
Serializer.serialize('json', res, use_natural_keys=True)
I get an error, TypeError: Equipment: eq1 is not JSON serializable
I have also tried wadofstuff for this serialization of foreign keys, but apparently in Django1.5, there is a clash between simplejson and json, and the query was throwing an error. So I am back to square one.
Any help will be highly appereciated. I have been splitting my hair for hours.