Ticket #14096: base.py.patch

File base.py.patch, 2.1 KB (added by Arthur Pemberton, 14 years ago)

more complete, tested patch

  • base.py

     
    444444        need for overrides of save() to pass around internal-only parameters
    445445        ('raw', 'cls', and 'origin').
    446446        """
     447        newpk = False # if a new primary key is created, it is stored here for returning
    447448        using = using or router.db_for_write(self.__class__, instance=self)
    448449        connection = connections[using]
    449450        assert not (force_insert and force_update)
     
    476477                if field and getattr(self, parent._meta.pk.attname) is None and getattr(self, field.attname) is not None:
    477478                    setattr(self, parent._meta.pk.attname, getattr(self, field.attname))
    478479
    479                 self.save_base(cls=parent, origin=org, using=using)
     480                newpk = self.save_base(cls=parent, origin=org, using=using)
    480481
     482                # reset primary key of the model and its parent share
     483                if self.__class__._meta.pk.attname == parent._meta.pk.attname:
     484                    self._set_pk_val(None)
     485
    481486                if field:
    482                     setattr(self, field.attname, self._get_pk_val(parent._meta))
     487                    setattr(self, field.attname, newpk)
    483488            if meta.proxy:
    484489                return
    485490
     
    531536                    result = manager._insert([(meta.pk, connection.ops.pk_default_value())], return_id=update_pk, raw_values=True, using=using)
    532537
    533538                if update_pk:
    534                     setattr(self, meta.pk.attname, result)
     539                    newpk = result
     540                    if cls == self.__class__:
     541                        setattr(self, meta.pk.attname, result)
    535542            transaction.commit_unless_managed(using=using)
    536543
    537544        # Store the database on which the object was saved
     
    542549            signals.post_save.send(sender=origin, instance=self,
    543550                created=(not record_exists), raw=raw, using=using)
    544551
     552        return newpk
     553
    545554    save_base.alters_data = True
    546555
    547556    def _collect_sub_objects(self, seen_objs, parent=None, nullable=False):
Back to Top