Django

Code

Changeset 7342

Show
Ignore:
Timestamp:
03/21/08 05:59:25 (5 months ago)
Author:
mtredinnick
Message:

queryset-refactor: Added some sanity checking to and() and or() because
some people insist on trying to merge a Queryset and a ValuesQuerySet?.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/queryset-refactor/django/db/models/query.py

    r7341 r7342  
    119119            raise IndexError, e.args 
    120120 
     121    def _merge_sanity_check(self, other): 
     122        """ 
     123        Checks that we are merging two comparable queyrset classes. 
     124        """ 
     125        if self.__class__ is not other.__class__: 
     126            raise TypeError("Cannot merge querysets of different types ('%s' and '%s'." 
     127                    % (self.__class__.__name__, other.__class__.__name__)) 
     128 
    121129    def __and__(self, other): 
     130        self._merge_sanity_check(other) 
    122131        combined = self._clone() 
    123132        combined.query.combine(other.query, sql.AND) 
     
    125134 
    126135    def __or__(self, other): 
     136        self._merge_sanity_check(other) 
    127137        combined = self._clone() 
    128138        combined.query.combine(other.query, sql.OR) 
     
    528538        return c 
    529539 
     540    def _merge_sanity_check(self, other): 
     541        super(ValuesQuerySet, self)._merge_sanity_check(other) 
     542        if (set(self.extra_names) != set(other.extra_names) or 
     543                set(self.field_names) != set(other.field_names)): 
     544            raise TypeError("Merging '%s' classes must involve the same values in each case." 
     545                    % self.__class__.__name__) 
     546 
    530547class ValuesListQuerySet(ValuesQuerySet): 
    531548    def iterator(self):