Opened 16 years ago
Last modified 13 years ago
#10515 closed
Add __rand__ and __ror__ to QuerySet [PATCH] — at Version 2
Reported by: | Sebastian Noack | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.0 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Design decision needed | |
Has patch: | yes | Needs documentation: | yes |
Needs tests: | yes | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
It would be nice, if the QuerySet would respect __rand__
and __ror__
, when combining it with other QuerySets. Note following example:
class PublicArticleQuerySet(QuerySet): def iterator(self): for obj in super(VisibleQuerySet, self).iterator(): obj._is_public = True yield obj def __or__(self, other): self._merge_sanity_check(other) # If a PublicArticleQuerySet is or'ed with an other QuerySet # we have to clone the other instead of this. So a QuerySet # is returned instead of a PublicArticleQuerySet, unless both # are PublicArticleQuerySets. combined = other._clone() combined.query.combine(self.query, sql.OR) return combined def __and__(self, other): self._merge_sanity_check(other) # If a PublicArticleQuerySet is and'ed with an other QuerySet # we have to clone this instead of the other. So allways a # PublicArticleQuerySet is returned. combined = self._clone() combined.query.combine(other.query, sql.AND) return combined # We want the behaviour described, also when combining vice versa. __ror__ = __or__ __rand__ = __rand__ class PublicArticleManager(models.Manager): def get_query_set(self): return PublicArticleQuerySet(self.model).filter(...) class Article(models.Model): ... objects = models.Manager() public_objects = PublicArticleManager() def is_public(self): if hasattr(self, '_is_public'): return self._is_public ...
I have attached a patch which enables this feature.
Change History (3)
by , 16 years ago
Attachment: | added-__rand__-and-__ror__-support-to-QuerySet.patch added |
---|
comment:1 by , 16 years ago
Description: | modified (diff) |
---|---|
Needs documentation: | set |
Needs tests: | set |
comment:2 by , 16 years ago
Description: | modified (diff) |
---|
Note:
See TracTickets
for help on using tickets.
Some regression tests, which also show why this useful, need to be added. A simplified example would probably be better, if possible.
There is of course the problem about what happens when you combine two custom QuerySets, both with __ror (or __rand) defined - which one should be used? Perhaps that is telling us something...