On Sun, Mar 29, 2009 at 9:49 PM, Kariem <kariem.huss...@gmail.com> wrote: > > I tried using the serialization as documented in > http://docs.djangoproject.com/en/dev/topics/serialization/ and do > something along the lines of > > json = serializers.get_serializer('json')() > json.serialize(o, ensure_ascii=False, fields=fields) > > "o" is a list of models that contain a reference to another model > (foreign key), similar to what is described in [1]: > > ### start > class MyModel(models.Model): > category = models.ForeignKey(Category) > ... > class Category(models.Model) > ... # name, and other attributes > ### end > > I have observed a somewhat unintuitive behavior and would like to ask, > whether this is intended, or I just did not see the right hint in the > documentation: > - If I pass "None" to the 'fields' argument in json.serialize, I > receive the key of the referenced category in the serialized result > along all other fields of MyModel and keys of other referenced models. > - If I pass "['category']" to the 'fields' argument in > json.serialize, the resulting fields are empty (in json: "fields": > { }) > > After stepping through the code, I found out that this behavior is > controlled in django.core.serializers.base.Serializer [2], and that I > can show the referenced category, if I pass 'categ' instead of > 'category' as field name; i.e. passing 'categ' in the fields argument > results in the json "fields": { "category": <key> } > > Could anybody explain why this is the case? Did I miss something in > the documentation, or should I have done something else to have the > keys of the referenced models included in the serialized result?
This sounds like you have found a bug. I certainly can't think of any reason that the behaviour your describe would be the intended behaviour. Please open a ticket and provide as much detail as possible so we can reproduce the bug - Django has existing tests for the fields argument to serializers (tests/modeltests/serializers/models.py, line 192) (around line 192), so we will need to work out what is different about your test case, and the more detail we have, the easier this will be. Better still, if you can provide a patch implementing a failing test case for the existing test suite, it will make the task of finding and fixing the problem much easier. Yours, Russ Magee %-) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---