1
0
mirror of https://github.com/django/django.git synced 2024-12-23 09:36:06 +00:00

Fixed #2108 -- do not try to save an empty model.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@3104 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2006-06-08 00:13:52 +00:00
parent 58ab678d35
commit 89920e058f
3 changed files with 23 additions and 5 deletions

View File

@ -183,11 +183,12 @@ class Model(object):
placeholders.append('(SELECT COUNT(*) FROM %s WHERE %s = %%s)' % \ placeholders.append('(SELECT COUNT(*) FROM %s WHERE %s = %%s)' % \
(backend.quote_name(self._meta.db_table), backend.quote_name(self._meta.order_with_respect_to.column))) (backend.quote_name(self._meta.db_table), backend.quote_name(self._meta.order_with_respect_to.column)))
db_values.append(getattr(self, self._meta.order_with_respect_to.attname)) db_values.append(getattr(self, self._meta.order_with_respect_to.attname))
cursor.execute("INSERT INTO %s (%s) VALUES (%s)" % \ if db_values:
(backend.quote_name(self._meta.db_table), ','.join(field_names), cursor.execute("INSERT INTO %s (%s) VALUES (%s)" % \
','.join(placeholders)), db_values) (backend.quote_name(self._meta.db_table), ','.join(field_names),
if self._meta.has_auto_field and not pk_set: ','.join(placeholders)), db_values)
setattr(self, self._meta.pk.attname, backend.get_last_insert_id(cursor, self._meta.db_table, self._meta.pk.column)) if self._meta.has_auto_field and not pk_set:
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()
# Run any post-save hooks. # Run any post-save hooks.

View File

View File

@ -0,0 +1,17 @@
"""
Empty model tests
These test that things behave sensibly for the rare corner-case of a model with
no fields.
"""
from django.db import models
class Empty(models.Model):
pass
API_TESTS = """
>>> m = Empty()
>>> m.save()
"""