Ticket #5309: base.py.2.diff

File base.py.2.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
    101103        dispatcher.send(signal=signals.pre_init, sender=self.__class__, args=args, kwargs=kwargs)
    102104
    103105        # There is a rather weird disparity here; if kwargs, it's set, then args
     
    214216        # Note: the comparison with '' is required for compatibility with
    215217        # oldforms-style model creation.
    216218        pk_set = pk_val is not None and pk_val != u''
    217         record_exists = True
    218         if pk_set:
     219        record_exists = False
     220        if self._is_stored is not False:
    219221            # Determine whether a record with the primary key already exists.
    220222            cursor.execute("SELECT 1 FROM %s WHERE %s=%%s" % \
    221223                (qn(self._meta.db_table), qn(self._meta.pk.column)),
     
    229231                        ','.join(['%s=%%s' % qn(f.column) for f in non_pks]),
    230232                        qn(self._meta.pk.column)),
    231233                        db_values + self._meta.pk.get_db_prep_lookup('exact', pk_val))
    232             else:
    233                 record_exists = False
    234         if not pk_set or not record_exists:
     234                    record_exists = True
     235        if not record_exists:
    235236            field_names = [qn(f.column) for f in self._meta.fields if not isinstance(f, AutoField)]
    236237            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)]
    237238            # If the PK has been manually set, respect that.
     
    256257                     connection.ops.pk_default_value()))
    257258            if self._meta.has_auto_field and not pk_set:
    258259                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
    259261        transaction.commit_unless_managed()
    260262
    261263        # Run any post-save hooks.
Back to Top