Fixed #19746 -- Allow deserialization of pk-less data
This commit is contained in:
parent
5a3d9490f1
commit
278dad5b41
|
@ -88,7 +88,7 @@ def Deserializer(object_list, **options):
|
||||||
for d in object_list:
|
for d in object_list:
|
||||||
# Look up the model and starting build a dict of data for it.
|
# Look up the model and starting build a dict of data for it.
|
||||||
Model = _get_model(d["model"])
|
Model = _get_model(d["model"])
|
||||||
data = {Model._meta.pk.attname: Model._meta.pk.to_python(d["pk"])}
|
data = {Model._meta.pk.attname: Model._meta.pk.to_python(d.get("pk", None))}
|
||||||
m2m_data = {}
|
m2m_data = {}
|
||||||
model_fields = Model._meta.get_all_field_names()
|
model_fields = Model._meta.get_all_field_names()
|
||||||
|
|
||||||
|
|
|
@ -117,6 +117,16 @@ object and any associated relationship data.
|
||||||
|
|
||||||
Calling ``DeserializedObject.save()`` saves the object to the database.
|
Calling ``DeserializedObject.save()`` saves the object to the database.
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
|
||||||
|
If the ``pk`` attribute in the serialized data doesn't exist or is
|
||||||
|
null, a new instance will be saved to the database.
|
||||||
|
|
||||||
|
.. versionchanged:: 1.6
|
||||||
|
|
||||||
|
In previous versions of Django, the ``pk`` attribute had to be present
|
||||||
|
on the serialized data or a ``DeserializationError`` would be raised.
|
||||||
|
|
||||||
This ensures that deserializing is a non-destructive operation even if the
|
This ensures that deserializing is a non-destructive operation even if the
|
||||||
data in your serialized representation doesn't match what's currently in the
|
data in your serialized representation doesn't match what's currently in the
|
||||||
database. Usually, working with these ``DeserializedObject`` instances looks
|
database. Usually, working with these ``DeserializedObject`` instances looks
|
||||||
|
|
|
@ -255,7 +255,7 @@ class SerializersTestBase(object):
|
||||||
for obj in deserial_objs:
|
for obj in deserial_objs:
|
||||||
self.assertFalse(obj.object.id)
|
self.assertFalse(obj.object.id)
|
||||||
obj.save()
|
obj.save()
|
||||||
self.assertEqual(Category.objects.all().count(), 4)
|
self.assertEqual(Category.objects.all().count(), 5)
|
||||||
|
|
||||||
|
|
||||||
class SerializersTransactionTestBase(object):
|
class SerializersTransactionTestBase(object):
|
||||||
|
@ -290,6 +290,9 @@ class XmlSerializerTestCase(SerializersTestBase, TestCase):
|
||||||
<object model="serializers.category">
|
<object model="serializers.category">
|
||||||
<field type="CharField" name="name">Reference</field>
|
<field type="CharField" name="name">Reference</field>
|
||||||
</object>
|
</object>
|
||||||
|
<object model="serializers.category">
|
||||||
|
<field type="CharField" name="name">Non-fiction</field>
|
||||||
|
</object>
|
||||||
</django-objects>"""
|
</django-objects>"""
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -351,7 +354,15 @@ class XmlSerializerTransactionTestCase(SerializersTransactionTestBase, Transacti
|
||||||
|
|
||||||
class JsonSerializerTestCase(SerializersTestBase, TestCase):
|
class JsonSerializerTestCase(SerializersTestBase, TestCase):
|
||||||
serializer_name = "json"
|
serializer_name = "json"
|
||||||
pkless_str = """[{"pk": null, "model": "serializers.category", "fields": {"name": "Reference"}}]"""
|
pkless_str = """[
|
||||||
|
{
|
||||||
|
"pk": null,
|
||||||
|
"model": "serializers.category",
|
||||||
|
"fields": {"name": "Reference"}
|
||||||
|
}, {
|
||||||
|
"model": "serializers.category",
|
||||||
|
"fields": {"name": "Non-fiction"}
|
||||||
|
}]"""
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _validate_output(serial_str):
|
def _validate_output(serial_str):
|
||||||
|
@ -433,6 +444,9 @@ else:
|
||||||
pkless_str = """- fields:
|
pkless_str = """- fields:
|
||||||
name: Reference
|
name: Reference
|
||||||
pk: null
|
pk: null
|
||||||
|
model: serializers.category
|
||||||
|
- fields:
|
||||||
|
name: Non-fiction
|
||||||
model: serializers.category"""
|
model: serializers.category"""
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
Loading…
Reference in New Issue