mirror of
https://github.com/django/django.git
synced 2025-06-06 20:19:13 +00:00
Fixed #2160 -- Modified object save logic to check for `pk is None
, rather than
bool(pk) == False
`. This allows primary keys to take a value of 0. Thanks for the report, fgutierrez@aureal.com.pe.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4459 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
695302ce92
commit
795de3a67c
@ -167,9 +167,8 @@ class Model(object):
|
|||||||
|
|
||||||
# First, try an UPDATE. If that doesn't update anything, do an INSERT.
|
# First, try an UPDATE. If that doesn't update anything, do an INSERT.
|
||||||
pk_val = self._get_pk_val()
|
pk_val = self._get_pk_val()
|
||||||
pk_set = bool(pk_val)
|
|
||||||
record_exists = True
|
record_exists = True
|
||||||
if pk_set:
|
if pk_val is not None:
|
||||||
# Determine whether a record with the primary key already exists.
|
# Determine whether a record with the primary key already exists.
|
||||||
cursor.execute("SELECT 1 FROM %s WHERE %s=%%s LIMIT 1" % \
|
cursor.execute("SELECT 1 FROM %s WHERE %s=%%s LIMIT 1" % \
|
||||||
(backend.quote_name(self._meta.db_table), backend.quote_name(self._meta.pk.column)), [pk_val])
|
(backend.quote_name(self._meta.db_table), backend.quote_name(self._meta.pk.column)), [pk_val])
|
||||||
@ -184,11 +183,11 @@ class Model(object):
|
|||||||
db_values + [pk_val])
|
db_values + [pk_val])
|
||||||
else:
|
else:
|
||||||
record_exists = False
|
record_exists = False
|
||||||
if not pk_set or not record_exists:
|
if pk_val is None or not record_exists:
|
||||||
field_names = [backend.quote_name(f.column) for f in self._meta.fields if not isinstance(f, AutoField)]
|
field_names = [backend.quote_name(f.column) for f in self._meta.fields if not isinstance(f, AutoField)]
|
||||||
db_values = [f.get_db_prep_save(f.pre_save(self, True)) for f in self._meta.fields if not isinstance(f, AutoField)]
|
db_values = [f.get_db_prep_save(f.pre_save(self, True)) for f in self._meta.fields if not isinstance(f, AutoField)]
|
||||||
# If the PK has been manually set, respect that.
|
# If the PK has been manually set, respect that.
|
||||||
if pk_set:
|
if pk_val is not None:
|
||||||
field_names += [f.column for f in self._meta.fields if isinstance(f, AutoField)]
|
field_names += [f.column for f in self._meta.fields if isinstance(f, AutoField)]
|
||||||
db_values += [f.get_db_prep_save(f.pre_save(self, True)) for f in self._meta.fields if isinstance(f, AutoField)]
|
db_values += [f.get_db_prep_save(f.pre_save(self, True)) for f in self._meta.fields if isinstance(f, AutoField)]
|
||||||
placeholders = ['%s'] * len(field_names)
|
placeholders = ['%s'] * len(field_names)
|
||||||
@ -208,7 +207,7 @@ class Model(object):
|
|||||||
(backend.quote_name(self._meta.db_table),
|
(backend.quote_name(self._meta.db_table),
|
||||||
backend.quote_name(self._meta.pk.column),
|
backend.quote_name(self._meta.pk.column),
|
||||||
backend.get_pk_default_value()))
|
backend.get_pk_default_value()))
|
||||||
if self._meta.has_auto_field and not pk_set:
|
if self._meta.has_auto_field and pk_val is None:
|
||||||
setattr(self, self._meta.pk.attname, backend.get_last_insert_id(cursor, self._meta.db_table, self._meta.pk.column))
|
setattr(self, self._meta.pk.attname, backend.get_last_insert_id(cursor, self._meta.db_table, self._meta.pk.column))
|
||||||
transaction.commit_unless_managed()
|
transaction.commit_unless_managed()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user