Ticket #13930: pk_natural_key.diff
File pk_natural_key.diff, 1.7 KB (added by , 14 years ago) |
---|
-
django/core/serializers/python.py
27 27 self._current = {} 28 28 29 29 def end_object(self, obj): 30 if self.use_natural_keys and hasattr(obj, 'natural_key'): 31 pk = obj.natural_key() 32 else: 33 pk = smart_unicode(obj._get_pk_val(), strings_only=True) 34 30 35 self.objects.append({ 31 36 "model" : smart_unicode(obj._meta), 32 "pk" : smart_unicode(obj._get_pk_val(), strings_only=True),37 "pk" : pk, 33 38 "fields" : self._current 34 39 }) 35 40 self._current = None … … 82 87 for d in object_list: 83 88 # Look up the model and starting build a dict of data for it. 84 89 Model = _get_model(d["model"]) 85 data = {Model._meta.pk.attname : Model._meta.pk.to_python(d["pk"])} 90 91 # Handle PK 92 if hasattr(Model._default_manager, 'get_by_natural_key') and hasattr(d["pk"], '__iter__'): 93 try: 94 # If there is an instance for this natural key in the database, use its existing pk 95 obj = Model._default_manager.db_manager(db).get_by_natural_key(*d["pk"]) 96 data = {Model._meta.pk.attname : obj.pk} 97 except Model.DoesNotExist: 98 # If there is not, assume that the pk will be generated automatically 99 data = {} 100 else: 101 data = {Model._meta.pk.attname : Model._meta.pk.to_python(d["pk"])} 102 86 103 m2m_data = {} 87 104 88 105 # Handle each field