mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-22 17:47:04 +03:00
Added enhancement for pk reference in many=True relations
This commit is contained in:
parent
8a58b1a380
commit
7ac3c3fff7
|
@ -341,6 +341,7 @@ class ManyRelatedField(Field):
|
|||
assert child_relation is not None, '`child_relation` is a required argument.'
|
||||
super(ManyRelatedField, self).__init__(*args, **kwargs)
|
||||
self.child_relation.bind(field_name='', parent=self)
|
||||
self.id_field = kwargs.pop('id_field', 'pk')
|
||||
|
||||
def get_value(self, dictionary):
|
||||
# We override the default field access in order to support
|
||||
|
@ -362,7 +363,7 @@ class ManyRelatedField(Field):
|
|||
|
||||
def get_attribute(self, instance):
|
||||
# Can't have any relationships if not created
|
||||
if not instance.pk:
|
||||
if getattr(instance, self.id_field) is None:
|
||||
return []
|
||||
|
||||
relationship = get_attribute(instance, self.source_attrs)
|
||||
|
|
Loading…
Reference in New Issue
Block a user