mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-17 03:51:03 +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.'
|
assert child_relation is not None, '`child_relation` is a required argument.'
|
||||||
super(ManyRelatedField, self).__init__(*args, **kwargs)
|
super(ManyRelatedField, self).__init__(*args, **kwargs)
|
||||||
self.child_relation.bind(field_name='', parent=self)
|
self.child_relation.bind(field_name='', parent=self)
|
||||||
|
self.id_field = kwargs.pop('id_field', 'pk')
|
||||||
|
|
||||||
def get_value(self, dictionary):
|
def get_value(self, dictionary):
|
||||||
# We override the default field access in order to support
|
# We override the default field access in order to support
|
||||||
|
@ -362,7 +363,7 @@ class ManyRelatedField(Field):
|
||||||
|
|
||||||
def get_attribute(self, instance):
|
def get_attribute(self, instance):
|
||||||
# Can't have any relationships if not created
|
# Can't have any relationships if not created
|
||||||
if not instance.pk:
|
if getattr(instance, self.id_field) is None:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
relationship = get_attribute(instance, self.source_attrs)
|
relationship = get_attribute(instance, self.source_attrs)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user