Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
  • Loading branch information...
commit 89920e058fcb673ff82ae67a41566216895c8bd3 1 parent 58ab678
Malcolm Tredinnick malcolmt authored
11 django/db/models/base.py
View
@@ -183,11 +183,12 @@ def save(self):
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)))
db_values.append(getattr(self, self._meta.order_with_respect_to.attname))
- cursor.execute("INSERT INTO %s (%s) VALUES (%s)" % \
- (backend.quote_name(self._meta.db_table), ','.join(field_names),
- ','.join(placeholders)), db_values)
- 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))
+ if db_values:
+ cursor.execute("INSERT INTO %s (%s) VALUES (%s)" % \
+ (backend.quote_name(self._meta.db_table), ','.join(field_names),
+ ','.join(placeholders)), db_values)
+ 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()
# Run any post-save hooks.
0  tests/modeltests/empty/__init__.py
View
No changes.
17 tests/modeltests/empty/models.py
View
@@ -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()
+
+"""
Please sign in to comment.
Something went wrong with that request. Please try again.