mirror of https://github.com/django/django.git
Fixed #757: manually set AutoField values are now respected; also added unit test to verify the correct behavior. Thanks, cygnus.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@1511 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
bf0f6ec3b4
commit
c28daf2289
1
AUTHORS
1
AUTHORS
|
@ -87,7 +87,6 @@ answer newbie questions, and generally made Django that much better:
|
|||
wojtek
|
||||
ye7cakf02@sneakemail.com
|
||||
|
||||
|
||||
A big THANK YOU goes to:
|
||||
|
||||
Rob Curley and Ralph Gage for letting us open-source Django.
|
||||
|
|
|
@ -1009,8 +1009,12 @@ def method_save(opts, self):
|
|||
record_exists = False
|
||||
if not pk_set or not record_exists:
|
||||
field_names = [db.db.quote_name(f.column) for f in opts.fields if not isinstance(f, AutoField)]
|
||||
placeholders = ['%s'] * len(field_names)
|
||||
db_values = [f.get_db_prep_save(f.pre_save(getattr(self, f.attname), True)) for f in opts.fields if not isinstance(f, AutoField)]
|
||||
# If the PK has been manually set we must respect that
|
||||
if pk_set:
|
||||
field_names += [f.column for f in opts.fields if isinstance(f, AutoField)]
|
||||
db_values += [f.get_db_prep_save(f.pre_save(getattr(self, f.column), True)) for f in opts.fields if isinstance(f, AutoField)]
|
||||
placeholders = ['%s'] * len(field_names)
|
||||
if opts.order_with_respect_to:
|
||||
field_names.append(db.db.quote_name('_order'))
|
||||
# TODO: This assumes the database supports subqueries.
|
||||
|
@ -1020,7 +1024,7 @@ def method_save(opts, self):
|
|||
cursor.execute("INSERT INTO %s (%s) VALUES (%s)" % \
|
||||
(db.db.quote_name(opts.db_table), ','.join(field_names),
|
||||
','.join(placeholders)), db_values)
|
||||
if opts.has_auto_field:
|
||||
if opts.has_auto_field and not pk_set:
|
||||
setattr(self, opts.pk.attname, db.get_last_insert_id(cursor, opts.db_table, opts.pk.column))
|
||||
db.db.commit()
|
||||
# Run any post-save hooks.
|
||||
|
|
|
@ -157,6 +157,14 @@ datetime.datetime(2005, 7, 31, 12, 30, 45)
|
|||
>>> a8.save()
|
||||
>>> a8.id
|
||||
8L
|
||||
|
||||
# You can manually specify the primary key when creating a new objet
|
||||
>>> a101 = articles.Article(id=101, headline='Article 101', pub_date=datetime(2005, 7, 31, 12, 30, 45))
|
||||
>>> a101.save()
|
||||
>>> a101 = articles.get_object(pk=101)
|
||||
>>> a101.headline
|
||||
'Article 101'
|
||||
|
||||
"""
|
||||
|
||||
from django.conf import settings
|
||||
|
|
Loading…
Reference in New Issue