Ticket #5309: base.py.diff

File base.py.diff, 2.1 KB (added by David Cramer <dcramer@…>, 17 years ago)
  • django/django/db/models/base.py

     
    9898        return not self.__eq__(other)
    9999
    100100    def __init__(self, *args, **kwargs):
    101         # We use this to keep track if the row has already been saved in the db.
    102         self.is_stored = None
    103101        dispatcher.send(signal=signals.pre_init, sender=self.__class__, args=args, kwargs=kwargs)
    104102
    105103        # There is a rather weird disparity here; if kwargs, it's set, then args
     
    216214        # Note: the comparison with '' is required for compatibility with
    217215        # oldforms-style model creation.
    218216        pk_set = pk_val is not None and pk_val != u''
    219         record_exists = False
    220         if self.is_stored is not False:
     217        record_exists = True
     218        if pk_set:
    221219            # Determine whether a record with the primary key already exists.
    222220            cursor.execute("SELECT 1 FROM %s WHERE %s=%%s" % \
    223221                (qn(self._meta.db_table), qn(self._meta.pk.column)),
     
    231229                        ','.join(['%s=%%s' % qn(f.column) for f in non_pks]),
    232230                        qn(self._meta.pk.column)),
    233231                        db_values + self._meta.pk.get_db_prep_lookup('exact', pk_val))
    234                     record_exists = True
    235         if not record_exists:
     232            else:
     233                record_exists = False
     234        if not pk_set or not record_exists:
    236235            field_names = [qn(f.column) for f in self._meta.fields if not isinstance(f, AutoField)]
    237236            db_values = [f.get_db_prep_save(raw and getattr(self, f.attname) or f.pre_save(self, True)) for f in self._meta.fields if not isinstance(f, AutoField)]
    238237            # If the PK has been manually set, respect that.
     
    257256                     connection.ops.pk_default_value()))
    258257            if self._meta.has_auto_field and not pk_set:
    259258                setattr(self, self._meta.pk.attname, connection.ops.last_insert_id(cursor, self._meta.db_table, self._meta.pk.column))
    260             self.is_stored = True
    261259        transaction.commit_unless_managed()
    262260
    263261        # Run any post-save hooks.
Back to Top