mirror of
https://github.com/django/django.git
synced 2025-07-05 18:29:11 +00:00
queryset-refactor: Added some sanity checking to __and__() and __or__() because
some people insist on trying to merge a Queryset and a ValuesQuerySet. git-svn-id: http://code.djangoproject.com/svn/django/branches/queryset-refactor@7342 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
334279f8dd
commit
e401ff8ff8
@ -118,12 +118,22 @@ class _QuerySet(object):
|
||||
except self.model.DoesNotExist, e:
|
||||
raise IndexError, e.args
|
||||
|
||||
def _merge_sanity_check(self, other):
|
||||
"""
|
||||
Checks that we are merging two comparable queyrset classes.
|
||||
"""
|
||||
if self.__class__ is not other.__class__:
|
||||
raise TypeError("Cannot merge querysets of different types ('%s' and '%s'."
|
||||
% (self.__class__.__name__, other.__class__.__name__))
|
||||
|
||||
def __and__(self, other):
|
||||
self._merge_sanity_check(other)
|
||||
combined = self._clone()
|
||||
combined.query.combine(other.query, sql.AND)
|
||||
return combined
|
||||
|
||||
def __or__(self, other):
|
||||
self._merge_sanity_check(other)
|
||||
combined = self._clone()
|
||||
combined.query.combine(other.query, sql.OR)
|
||||
return combined
|
||||
@ -527,6 +537,13 @@ class ValuesQuerySet(QuerySet):
|
||||
c._setup_query()
|
||||
return c
|
||||
|
||||
def _merge_sanity_check(self, other):
|
||||
super(ValuesQuerySet, self)._merge_sanity_check(other)
|
||||
if (set(self.extra_names) != set(other.extra_names) or
|
||||
set(self.field_names) != set(other.field_names)):
|
||||
raise TypeError("Merging '%s' classes must involve the same values in each case."
|
||||
% self.__class__.__name__)
|
||||
|
||||
class ValuesListQuerySet(ValuesQuerySet):
|
||||
def iterator(self):
|
||||
self.field_names.extend([f for f in self.query.extra_select.keys()])
|
||||
|
Loading…
x
Reference in New Issue
Block a user