mirror of https://github.com/django/django.git
Fixed #24854 -- Refactored QuerySet._values
Added django.db.models.sql.query.Query.set_values() method to handle query mutation.
This commit is contained in:
parent
7549eb0004
commit
698be78d74
|
@ -681,42 +681,7 @@ class QuerySet(object):
|
|||
def _values(self, *fields):
|
||||
clone = self._clone()
|
||||
clone._fields = fields
|
||||
|
||||
query = clone.query
|
||||
query.select_related = False
|
||||
query.clear_deferred_loading()
|
||||
query.clear_select_fields()
|
||||
|
||||
if query.group_by is True:
|
||||
query.add_fields((f.attname for f in self.model._meta.concrete_fields), False)
|
||||
query.set_group_by()
|
||||
query.clear_select_fields()
|
||||
|
||||
if fields:
|
||||
field_names = []
|
||||
extra_names = []
|
||||
annotation_names = []
|
||||
if not query._extra and not query._annotations:
|
||||
# Shortcut - if there are no extra or annotations, then
|
||||
# the values() clause must be just field names.
|
||||
field_names = list(fields)
|
||||
else:
|
||||
query.default_cols = False
|
||||
for f in fields:
|
||||
if f in query.extra_select:
|
||||
extra_names.append(f)
|
||||
elif f in query.annotation_select:
|
||||
annotation_names.append(f)
|
||||
else:
|
||||
field_names.append(f)
|
||||
query.set_extra_mask(extra_names)
|
||||
query.set_annotation_mask(annotation_names)
|
||||
else:
|
||||
field_names = [f.attname for f in self.model._meta.concrete_fields]
|
||||
|
||||
query.values_select = field_names
|
||||
query.add_fields(field_names, True)
|
||||
|
||||
clone.query.set_values(fields)
|
||||
return clone
|
||||
|
||||
def values(self, *fields):
|
||||
|
|
|
@ -1847,6 +1847,41 @@ class Query(object):
|
|||
self.extra_select_mask = set(names)
|
||||
self._extra_select_cache = None
|
||||
|
||||
def set_values(self, fields):
|
||||
self.select_related = False
|
||||
self.clear_deferred_loading()
|
||||
self.clear_select_fields()
|
||||
|
||||
if self.group_by is True:
|
||||
self.add_fields((f.attname for f in self.model._meta.concrete_fields), False)
|
||||
self.set_group_by()
|
||||
self.clear_select_fields()
|
||||
|
||||
if fields:
|
||||
field_names = []
|
||||
extra_names = []
|
||||
annotation_names = []
|
||||
if not self._extra and not self._annotations:
|
||||
# Shortcut - if there are no extra or annotations, then
|
||||
# the values() clause must be just field names.
|
||||
field_names = list(fields)
|
||||
else:
|
||||
self.default_cols = False
|
||||
for f in fields:
|
||||
if f in self.extra_select:
|
||||
extra_names.append(f)
|
||||
elif f in self.annotation_select:
|
||||
annotation_names.append(f)
|
||||
else:
|
||||
field_names.append(f)
|
||||
self.set_extra_mask(extra_names)
|
||||
self.set_annotation_mask(annotation_names)
|
||||
else:
|
||||
field_names = [f.attname for f in self.model._meta.concrete_fields]
|
||||
|
||||
self.values_select = field_names
|
||||
self.add_fields(field_names, True)
|
||||
|
||||
@property
|
||||
def annotation_select(self):
|
||||
"""The OrderedDict of aggregate columns that are not masked, and should
|
||||
|
|
Loading…
Reference in New Issue