summaryrefslogtreecommitdiff
path: root/django/db/models
diff options
context:
space:
mode:
authorMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2009-03-01 01:59:18 +0000
committerMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2009-03-01 01:59:18 +0000
commit016f12a2c27fd6006d43cc32af734dfd0ea96f98 (patch)
tree5c3634e9f4d30461ca07824f5585099a6362033d /django/db/models
parent283c89e4c61fa99dfdf6688a12ed3ce5a3945347 (diff)
[1.0.X] Fixed insert/update handling when no database interaction is required.
Fixed #10205 as part of this. Backport of r9926 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@9927 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/db/models')
-rw-r--r--django/db/models/sql/query.py8
-rw-r--r--django/db/models/sql/subqueries.py4
2 files changed, 7 insertions, 5 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index 0a9092d77d..79bb2bafbf 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -1743,9 +1743,11 @@ class BaseQuery(object):
iterator over the results if the result_type is MULTI.
result_type is either MULTI (use fetchmany() to retrieve all rows),
- SINGLE (only retrieve a single row), or None (no results expected, but
- the cursor is returned, since it's used by subclasses such as
- InsertQuery).
+ SINGLE (only retrieve a single row), or None. In this last case, the
+ cursor is returned if any query is executed, since it's used by
+ subclasses such as InsertQuery). It's possible, however, that no query
+ is needed, as the filters describe an empty set. In that case, None is
+ returned, to avoid any unnecessary database interaction.
"""
try:
sql, params = self.as_sql()
diff --git a/django/db/models/sql/subqueries.py b/django/db/models/sql/subqueries.py
index 39ef439dc9..a84eeabf38 100644
--- a/django/db/models/sql/subqueries.py
+++ b/django/db/models/sql/subqueries.py
@@ -115,7 +115,7 @@ class UpdateQuery(Query):
tables, but their rowcounts are not returned).
"""
cursor = super(UpdateQuery, self).execute_sql(result_type)
- rows = cursor.rowcount
+ rows = cursor and cursor.rowcount or 0
del cursor
for query in self.get_related_updates():
query.execute_sql(result_type)
@@ -307,7 +307,7 @@ class InsertQuery(Query):
def execute_sql(self, return_id=False):
cursor = super(InsertQuery, self).execute_sql(None)
- if return_id:
+ if return_id and cursor:
return self.connection.ops.last_insert_id(cursor,
self.model._meta.db_table, self.model._meta.pk.column)