I've serialized my django model:
serializers.serialize(MyModel.objects.filter(color="Red"))
and got this output:
<object model="example.example" pk="133">
<field name="name" type="CharField">John Smith</field>
<field name="color" type="CharField">Red</field>
... #more fields
</object>
So you can see I have pk="133":
And now I want to deserialize this into django model again and save() into database but with different pk so it should create new record with new id.
I'm trying to parse XML and change pk using:
- pk="" - parser complains that pk should be integer
- pk="-1" or "0" - actually creates record with id/pk = "1" or "0"
- pk="None" or None or "null" - parser complains that pk should be integer
- remove "pk" attribute - parser complains that attribute is mandatory
In Django Serialization article there is an example how to deserialize from JSON with null "pk".
# You can easily create new objects by deserializing data with an empty PK
# (It's easier to demo this with JSON...)
>>> new_author_json = '[{"pk": null, "model": "serializers.author", "fields": {"name": "Bill"}}]'
>>> for obj in serializers.deserialize("json", new_author_json):
... obj.save()
(It's actually for 0.96, but I'm assuming it should work for 1.* also)
So in JSON pk can be null but in XML it complains. How to set pk to null for XML?
Thanks