mirror of
https://github.com/django/django.git
synced 2025-07-06 10:49:17 +00:00
queryset-refactor: Implemented filtering by output columns specified in
extra(select=...). Refs #4002. git-svn-id: http://code.djangoproject.com/svn/django/branches/queryset-refactor@6762 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
a2418176fd
commit
79653a4148
@ -692,8 +692,16 @@ class Query(object):
|
|||||||
opts = self.model._meta
|
opts = self.model._meta
|
||||||
alias = self.join((None, opts.db_table, None, None))
|
alias = self.join((None, opts.db_table, None, None))
|
||||||
|
|
||||||
|
try:
|
||||||
field, target, unused, join_list, nullable = self.setup_joins(parts,
|
field, target, unused, join_list, nullable = self.setup_joins(parts,
|
||||||
opts, alias, (connection == AND))
|
opts, alias, (connection == AND))
|
||||||
|
except TypeError, e:
|
||||||
|
if len(parts) != 1 or parts[0] not in self.extra_select:
|
||||||
|
raise e
|
||||||
|
# Filtering on some alias from extra(select=...)
|
||||||
|
self.where.add([None, parts[0], None, lookup_type, value],
|
||||||
|
connection)
|
||||||
|
return
|
||||||
col = target.column
|
col = target.column
|
||||||
alias = join_list[-1][-1]
|
alias = join_list[-1][-1]
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ import datetime
|
|||||||
|
|
||||||
from django.utils import tree
|
from django.utils import tree
|
||||||
from django.db import connection
|
from django.db import connection
|
||||||
|
from django.db.models.fields import Field
|
||||||
from datastructures import EmptyResultSet
|
from datastructures import EmptyResultSet
|
||||||
|
|
||||||
# Connection types
|
# Connection types
|
||||||
@ -96,7 +97,10 @@ class WhereNode(tree.Node):
|
|||||||
else:
|
else:
|
||||||
format = '%s %s'
|
format = '%s %s'
|
||||||
|
|
||||||
|
if field:
|
||||||
params = field.get_db_prep_lookup(lookup_type, value)
|
params = field.get_db_prep_lookup(lookup_type, value)
|
||||||
|
else:
|
||||||
|
params = Field().get_db_prep_lookup(lookup_type, value)
|
||||||
|
|
||||||
if lookup_type in connection.operators:
|
if lookup_type in connection.operators:
|
||||||
return (format % (field_sql,
|
return (format % (field_sql,
|
||||||
|
@ -404,5 +404,18 @@ thus fail.)
|
|||||||
... params.reverse()
|
... params.reverse()
|
||||||
>>> Item.objects.extra(select=SortedDict(s), params=params).values('a','b')[0]
|
>>> Item.objects.extra(select=SortedDict(s), params=params).values('a','b')[0]
|
||||||
{'a': u'one', 'b': u'two'}
|
{'a': u'one', 'b': u'two'}
|
||||||
|
|
||||||
|
Bug #4002
|
||||||
|
Attributes used in extra(select=...) are available for use in subsequent
|
||||||
|
order_by() and filter() calls.
|
||||||
|
|
||||||
|
# Order by the number of tags attached to an item.
|
||||||
|
>>> l = Item.objects.extra(select={'count': 'select count(*) from queries_item_tags where queries_item_tags.item_id = queries_item.id'}).order_by('-count')
|
||||||
|
>>> [o.count for o in l]
|
||||||
|
[2, 2, 1, 0]
|
||||||
|
|
||||||
|
# Filter those items that have exactly one tag attacjed.
|
||||||
|
>>> Item.objects.extra(select={'count': 'select count(*) from queries_item_tags where queries_item_tags.item_id = queries_item.id'}).filter(count=1)
|
||||||
|
[<Item: four>]
|
||||||
"""}
|
"""}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user