mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +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:
		| @@ -118,12 +118,22 @@ class _QuerySet(object): | |||||||
|         except self.model.DoesNotExist, e: |         except self.model.DoesNotExist, e: | ||||||
|             raise IndexError, e.args |             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): |     def __and__(self, other): | ||||||
|  |         self._merge_sanity_check(other) | ||||||
|         combined = self._clone() |         combined = self._clone() | ||||||
|         combined.query.combine(other.query, sql.AND) |         combined.query.combine(other.query, sql.AND) | ||||||
|         return combined |         return combined | ||||||
|  |  | ||||||
|     def __or__(self, other): |     def __or__(self, other): | ||||||
|  |         self._merge_sanity_check(other) | ||||||
|         combined = self._clone() |         combined = self._clone() | ||||||
|         combined.query.combine(other.query, sql.OR) |         combined.query.combine(other.query, sql.OR) | ||||||
|         return combined |         return combined | ||||||
| @@ -527,6 +537,13 @@ class ValuesQuerySet(QuerySet): | |||||||
|             c._setup_query() |             c._setup_query() | ||||||
|         return c |         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): | class ValuesListQuerySet(ValuesQuerySet): | ||||||
|     def iterator(self): |     def iterator(self): | ||||||
|         self.field_names.extend([f for f in self.query.extra_select.keys()]) |         self.field_names.extend([f for f in self.query.extra_select.keys()]) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user