Hi list,
I'm busy writing a custom json serializer for django. It works very
good, only now I want to serialize ForeignKey and M2M relations inside
the same serialization call and build nested json objects with all model
instances that the current model instance relates to.
{'field_a': 'value',
'field_b': 'value',
'field_c': { <-- Nest Foreign Keys
'field_d': 'value',
'field_e': 'value',
},
'field_f': [ <-- Nest M2M
{'field_g': 'value'},
{'field_h': 'value'}
]
}
I found https://code.djangoproject.com/ticket/4656 that discusses this
problem, but, from what I understood, is not offering a clean solution yet.
So I actually have 2 questions:
1) Is there an "common way" how people do that? I'm sure other people
were doing thesame thing already. I found
http://code.google.com/p/wadofstuff/wiki/DjangoFullSerializers that
seems to do exactly this, but if possible I prefer not to use another
3rd party dependency.
2) I found 'a way' where I iterate through each field of the model and
test the field in the following way:
if type(a._meta.get_field('field_c')) == models.ForeignKey:
# follow relation and serialize it
Now is it a smart idea to access the model option with _meta, or can I
expect this not to work anymore with the next django release?
Any hint or input is appreciated.
greetinx
crito
--
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.