mirror of
https://github.com/django/django.git
synced 2025-07-06 02:39:12 +00:00
queryset-refactor: Optimised the SQL portion of Model.save().
git-svn-id: http://code.djangoproject.com/svn/django/branches/queryset-refactor@7249 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
ade818fd7d
commit
b102bf28b9
@ -315,11 +315,11 @@ class Model(object):
|
|||||||
update_pk = bool(meta.has_auto_field and not pk_set)
|
update_pk = bool(meta.has_auto_field and not pk_set)
|
||||||
if values:
|
if values:
|
||||||
# Create a new record.
|
# Create a new record.
|
||||||
result = manager._insert(_return_id=update_pk, **dict(values))
|
result = manager._insert(__return_id=update_pk, **dict(values))
|
||||||
else:
|
else:
|
||||||
# Create a new record with defaults for everything.
|
# Create a new record with defaults for everything.
|
||||||
result = manager._insert(_return_id=update_pk,
|
result = manager._insert(__return_id=update_pk,
|
||||||
_raw_values=True, pk=connection.ops.pk_default_value())
|
__raw_values=True, pk=connection.ops.pk_default_value())
|
||||||
|
|
||||||
if update_pk:
|
if update_pk:
|
||||||
setattr(self, meta.pk.attname, result)
|
setattr(self, meta.pk.attname, result)
|
||||||
|
@ -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.dispatch import dispatcher
|
||||||
from django.db.models import signals
|
from django.db.models import signals
|
||||||
from django.db.models.fields import FieldDoesNotExist
|
from django.db.models.fields import FieldDoesNotExist
|
||||||
@ -110,8 +110,8 @@ class Manager(object):
|
|||||||
def reverse(self, *args, **kwargs):
|
def reverse(self, *args, **kwargs):
|
||||||
return self.get_query_set().reverse(*args, **kwargs)
|
return self.get_query_set().reverse(*args, **kwargs)
|
||||||
|
|
||||||
def _insert(self, *args, **kwargs):
|
def _insert(self, **kwargs):
|
||||||
return self.get_query_set()._insert(*args, **kwargs)
|
return insert_query(self.model, **kwargs)
|
||||||
|
|
||||||
class ManagerDescriptor(object):
|
class ManagerDescriptor(object):
|
||||||
# This class ensures managers aren't accessible via model instances.
|
# This class ensures managers aren't accessible via model instances.
|
||||||
|
@ -457,18 +457,6 @@ class _QuerySet(object):
|
|||||||
except StopIteration:
|
except StopIteration:
|
||||||
self._iter = None
|
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.
|
# Use the backend's QuerySet class if it defines one. Otherwise, use _QuerySet.
|
||||||
if connection.features.uses_custom_queryset:
|
if connection.features.uses_custom_queryset:
|
||||||
QuerySet = connection.ops.query_set_class(_QuerySet)
|
QuerySet = connection.ops.query_set_class(_QuerySet)
|
||||||
@ -681,3 +669,13 @@ def delete_objects(seen_objs):
|
|||||||
|
|
||||||
transaction.commit_unless_managed()
|
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)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user