diff options
| author | Matt d'Entremont | 2015-03-04 17:36:03 -0400 | 
|---|---|---|
| committer | Matt d'Entremont | 2015-03-06 12:50:37 -0400 | 
| commit | fb58ef043cc39d900bb8389855f07087cb0d7920 (patch) | |
| tree | 79e80017c1f7535c7d9a592888ef6c0aa352e039 /rest_framework | |
| parent | f7917928c080aa0b055cbfc588f61ec01f16771c (diff) | |
| download | django-rest-framework-fb58ef043cc39d900bb8389855f07087cb0d7920.tar.bz2 | |
Add support for serializing models with m2m related fields
- In both ManyRelatedField, provide an empty return when trying to
  access a relation field if the instance in question has no PK (so
  likely hasn't been inserted yet)
- Add relevant tests
- Without these changes, exceptions would be raised when trying to
  serialize the uncreated models as it is impossible to query
  relations without a PK
- Add test to ensure RelatedField does not regress as currently 
  supports being serialized with and unsaved model
Diffstat (limited to 'rest_framework')
| -rw-r--r-- | rest_framework/relations.py | 4 | 
1 files changed, 4 insertions, 0 deletions
| diff --git a/rest_framework/relations.py b/rest_framework/relations.py index 0b7c9d86..3a966c5b 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -360,6 +360,10 @@ class ManyRelatedField(Field):          ]      def get_attribute(self, instance): +        # Can't have any relationships if not created +        if not instance.pk: +            return [] +          relationship = get_attribute(instance, self.source_attrs)          return relationship.all() if (hasattr(relationship, 'all')) else relationship | 
