Skip to content

Commit

Permalink
queryset-refactor: Optimised the SQL portion of Model.save().
Browse files Browse the repository at this point in the history
git-svn-id: http://code.djangoproject.com/svn/django/branches/queryset-refactor@7249 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
malcolmt committed Mar 15, 2008
1 parent ade818f commit b102bf2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
6 changes: 3 additions & 3 deletions django/db/models/base.py
Expand Up @@ -315,11 +315,11 @@ def save_base(self, raw=False, cls=None):
update_pk = bool(meta.has_auto_field and not pk_set)
if values:
# Create a new record.
result = manager._insert(_return_id=update_pk, **dict(values))
result = manager._insert(__return_id=update_pk, **dict(values))
else:
# Create a new record with defaults for everything.
result = manager._insert(_return_id=update_pk,
_raw_values=True, pk=connection.ops.pk_default_value())
result = manager._insert(__return_id=update_pk,
__raw_values=True, pk=connection.ops.pk_default_value())

if update_pk:
setattr(self, meta.pk.attname, result)
Expand Down
6 changes: 3 additions & 3 deletions django/db/models/manager.py
@@ -1,4 +1,4 @@
from django.db.models.query import QuerySet, EmptyQuerySet
from django.db.models.query import QuerySet, EmptyQuerySet, insert_query
from django.dispatch import dispatcher
from django.db.models import signals
from django.db.models.fields import FieldDoesNotExist
Expand Down Expand Up @@ -110,8 +110,8 @@ def update(self, *args, **kwargs):
def reverse(self, *args, **kwargs):
return self.get_query_set().reverse(*args, **kwargs)

def _insert(self, *args, **kwargs):
return self.get_query_set()._insert(*args, **kwargs)
def _insert(self, **kwargs):
return insert_query(self.model, **kwargs)

class ManagerDescriptor(object):
# This class ensures managers aren't accessible via model instances.
Expand Down
22 changes: 10 additions & 12 deletions django/db/models/query.py
Expand Up @@ -457,18 +457,6 @@ def _fill_cache(self, num=None):
except StopIteration:
self._iter = None

def _insert(self, _return_id=False, _raw_values=False, **kwargs):
"""
Inserts a new record for the given model. This provides an interface to
the InsertQuery class and is how Model.save() is implemented. It is not
part of the public API of QuerySet, though.
"""
self._result_cache = None
query = self.query.clone(sql.InsertQuery)
query.insert_values(kwargs, _raw_values)
return query.execute_sql(_return_id)
_insert.alters_data = True

# Use the backend's QuerySet class if it defines one. Otherwise, use _QuerySet.
if connection.features.uses_custom_queryset:
QuerySet = connection.ops.query_set_class(_QuerySet)
Expand Down Expand Up @@ -681,3 +669,13 @@ def delete_objects(seen_objs):

transaction.commit_unless_managed()

def insert_query(__model, __return_id=False, __raw_values=False, **kwargs):
"""
Inserts a new record for the given model. This provides an interface to
the InsertQuery class and is how Model.save() is implemented. It is not
part of the public API.
"""
query = sql.InsertQuery(__model, connection)
query.insert_values(kwargs, __raw_values)
return query.execute_sql(__return_id)

0 comments on commit b102bf2

Please sign in to comment.