Ticket #5309: base.py.diff
File base.py.diff, 2.1 KB (added by , 17 years ago) |
---|
-
django/django/db/models/base.py
98 98 return not self.__eq__(other) 99 99 100 100 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 = None103 101 dispatcher.send(signal=signals.pre_init, sender=self.__class__, args=args, kwargs=kwargs) 104 102 105 103 # There is a rather weird disparity here; if kwargs, it's set, then args … … 216 214 # Note: the comparison with '' is required for compatibility with 217 215 # oldforms-style model creation. 218 216 pk_set = pk_val is not None and pk_val != u'' 219 record_exists = False220 if self.is_stored is not False:217 record_exists = True 218 if pk_set: 221 219 # Determine whether a record with the primary key already exists. 222 220 cursor.execute("SELECT 1 FROM %s WHERE %s=%%s" % \ 223 221 (qn(self._meta.db_table), qn(self._meta.pk.column)), … … 231 229 ','.join(['%s=%%s' % qn(f.column) for f in non_pks]), 232 230 qn(self._meta.pk.column)), 233 231 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: 236 235 field_names = [qn(f.column) for f in self._meta.fields if not isinstance(f, AutoField)] 237 236 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)] 238 237 # If the PK has been manually set, respect that. … … 257 256 connection.ops.pk_default_value())) 258 257 if self._meta.has_auto_field and not pk_set: 259 258 setattr(self, self._meta.pk.attname, connection.ops.last_insert_id(cursor, self._meta.db_table, self._meta.pk.column)) 260 self.is_stored = True261 259 transaction.commit_unless_managed() 262 260 263 261 # Run any post-save hooks.