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