id summary reporter owner description type status component version severity resolution keywords cc stage has_patch needs_docs needs_tests needs_better_patch easy ui_ux 11486 "There are no way to provide empty (or null/None) ""pk"" for XML serialized object to deserialized" zdmytriv niall "I've serialized my django model: {{{ serializers.serialize(MyModel.objects.filter(color=""Red"")) }}} and got this output: {{{ John Smith Red ... #more fields }}} 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 http://www.djangoproject.com/documentation/0.96/models/serializers/ 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. Looks like bug. There are no way to provide empty (or null/None) ""pk"" for XML serialized object. From django/core/serializers/xml_serializer.py: {{{ class Deserializer(base.Deserializer): ... def _handle_object(self, node): ... pk = node.getAttribute(""pk"") if not pk: raise base.DeserializationError("" node is missing the 'pk' attribute"") data = {Model._meta.pk.attname : Model._meta.pk.to_python(pk)} ... }}} If pk attribute is missing - exception is raised. So we have to provide some pk. From django/models/fields/__init__.py {{{ class AutoField(Field): ... def to_python(self, value): if value is None: return value try: return int(value) except (TypeError, ValueError): raise exceptions.ValidationError( _(""This value must be an integer."")) ... }}} If pk is not integer - also exception. It looks like there are no way to provide empty(null/Node) pk." closed Core (Serialization) dev fixed xml, deserialization Accepted 1 0 0 0 0 0