I have a model that refers to a ForeignKey, e.g.:
class Template(models.Model):
type = models.ForeignKey(Type)
...
class Type(models.Model)
name = models.CharField(max_length=32)
...
I can fetch these objects fine using the model API.
t = Template.objects.get(id=1)
print t.type
>> <Type: Type object>
print t.type.id
>> 3
print t.type.name
>> TemplateType3
If I serialize these, I cannot get 'name.' I know Django lazily
retrieves these, but even by iterating or calling t.type.name, they
are unavailable during serialization. Is there any way to have Django
evaluate the ForeignKey object so that I can retrieve the field,
'name?' I need fields to contain "name": "TemplateType3" (or whatever
it may be)! :)
from django.core import serializers
print serializers.serialize('json', Template.objects.get(id=1))
>> {
"pk": 1,
"model": "template",
"fields": {
"type": 3
}
},
Thanks,
Adam
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---