mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-23 15:54:16 +03:00
Use either PrimaryKeyRelatedField or ManyPrimaryKeyRelatedField as appropriate (fixes test)
This commit is contained in:
parent
cab3b2f3f8
commit
58c1263267
|
@ -221,13 +221,13 @@ class PrimaryKeyRelatedField(RelatedField):
|
||||||
|
|
||||||
def field_to_native(self, obj, field_name):
|
def field_to_native(self, obj, field_name):
|
||||||
try:
|
try:
|
||||||
obj = obj.serializable_value(self.source or field_name)
|
pk = obj.serializable_value(self.source or field_name)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
# RelatedObject (reverse relationship)
|
# RelatedObject (reverse relationship)
|
||||||
obj = getattr(obj, self.source or field_name)
|
obj = getattr(obj, self.source or field_name)
|
||||||
return self.to_native(obj.pk)
|
return self.to_native(obj.pk)
|
||||||
# Forward relationship
|
# Forward relationship
|
||||||
return self.to_native(obj)
|
return self.to_native(pk)
|
||||||
|
|
||||||
def field_from_native(self, data, field_name, into):
|
def field_from_native(self, data, field_name, into):
|
||||||
value = data.get(field_name)
|
value = data.get(field_name)
|
||||||
|
@ -237,13 +237,13 @@ class PrimaryKeyRelatedField(RelatedField):
|
||||||
class ManyPrimaryKeyRelatedField(PrimaryKeyRelatedField):
|
class ManyPrimaryKeyRelatedField(PrimaryKeyRelatedField):
|
||||||
def field_to_native(self, obj, field_name):
|
def field_to_native(self, obj, field_name):
|
||||||
try:
|
try:
|
||||||
obj = obj.serializable_value(self.source or field_name)
|
queryset = obj.serializable_value(self.source or field_name)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
# RelatedManager (reverse relationship)
|
# RelatedManager (reverse relationship)
|
||||||
obj = getattr(obj, self.source or field_name)
|
queryset = getattr(obj, self.source or field_name)
|
||||||
return [self.to_native(item.pk) for item in obj.all()]
|
return [self.to_native(item.pk) for item in queryset.all()]
|
||||||
# Forward relationship
|
# Forward relationship
|
||||||
return [self.to_native(item.pk) for item in obj.all()]
|
return [self.to_native(item.pk) for item in queryset.all()]
|
||||||
|
|
||||||
def field_from_native(self, data, field_name, into):
|
def field_from_native(self, data, field_name, into):
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -351,6 +351,8 @@ class ModelSerializer(RelatedField, Serializer):
|
||||||
"""
|
"""
|
||||||
Creates a default instance of a flat relational field.
|
Creates a default instance of a flat relational field.
|
||||||
"""
|
"""
|
||||||
|
if isinstance(model_field, models.fields.related.ManyToManyField):
|
||||||
|
return ManyPrimaryKeyRelatedField()
|
||||||
return PrimaryKeyRelatedField()
|
return PrimaryKeyRelatedField()
|
||||||
|
|
||||||
def get_field(self, model_field):
|
def get_field(self, model_field):
|
||||||
|
|
|
@ -201,6 +201,7 @@ class ManyToManyTests(TestCase):
|
||||||
self.assertEquals(len(ManyToManyModel.objects.all()), 2)
|
self.assertEquals(len(ManyToManyModel.objects.all()), 2)
|
||||||
self.assertEquals(instance.pk, 2)
|
self.assertEquals(instance.pk, 2)
|
||||||
self.assertEquals(list(instance.rel.all()), [])
|
self.assertEquals(list(instance.rel.all()), [])
|
||||||
|
|
||||||
# def test_deserialization_for_update(self):
|
# def test_deserialization_for_update(self):
|
||||||
# serializer = self.serializer_class(self.data, instance=self.instance)
|
# serializer = self.serializer_class(self.data, instance=self.instance)
|
||||||
# expected = self.instance
|
# expected = self.instance
|
||||||
|
|
Loading…
Reference in New Issue
Block a user