tomchristie/django-rest-framework — pull request #1318 by sheppard (2013-12-24 05:34:08+00:00)
Title: saving nested generic relation serializers broken in django 1.6

It looks like In Django 1.6 reverse GenericRelations moved from `meta.many_to_many` to `meta.virtual_fields`. This change broke code that implicitly relied on many_to_many code to handle saving GenericRelations, for example via a nested serializer. The related ticket fixing this in Django 1.6.1 is django/django#1914 and https://code.djangoproject.com/ticket/21428 .

I've included a test that initially failed in Django 1.6 while succeeding in 1.5. I've also included a simple fix, which just includes `virtual_fields` in the m2m saving code in `restore_object`. The same approach could be used elsewhere, for example I had code that was relying on many_to_many to ensure subclasses of GenericRelation with `serialize=True` set would be included in `get_default_fields()`. I have a workaround for that case (wq/wq.db#9) but I could see adding the `virtual_fields` bit to the DRF get_default_fields() and other places the sake of consistency.

The one thing to keep in mind is that not every virtual field can necessarily be manipulated with the many to many API. My fix here assumes that if someone adds a field to their serializer that maps to a virtual field on the model, then it's probably a GenericRelation or something with an m2m API.

I know there is other work to improve (forward) generic foreign key and nested serializer support, but I was hoping this change would be small enough to be included in a patch release.