Feature proposal: Q.push(...) and QuerySet.filter(relfield=Q(...)) | Johannes Dollinger | 9/30/11 2:37 PM | The aim of this proposal is to reuse Q objects for models that are related through FK or M2M fields. A simplified example would be a Q object like >>> is_blue = Q(blue=True) that should be reused to filter the owners of things: >>> User.objects.filter(things__blue=True). Currently, the only way to reuse the Q object would be subquery: >>> User.objects.filter(things__in=Thing.objects.filter(is_blue)). I therefore propose the following API: >>> Q(lookup=value).push('relfield') which would be equivalent to >>> Q(relfield__lookup=value) and >>> qs.filter(relfield=Q(lookup=value)) which would be equivalent to >>> qs.filter(Q(lookup=value).push('relfield')). We then could write >>> User.objects.filter(things=is_blue) which is shorter and produces more natural SQL than the subquery solution. Thoughs? Suggestions? __ |
Re: Feature proposal: Q.push(...) and QuerySet.filter(relfield=Q(...)) | Calvin Spealman | 9/30/11 10:21 PM | On Fri, Sep 30, 2011 at 5:37 PM, Johannes Dollinger I don't like this. > which would be equivalent to I do like this > which would be equivalent to Again, not this. Are you only proposal the push method as an implementation detail? If > We then could write> -- > You received this message because you are subscribed to the Google Groups "Django developers" group. > To post to this group, send email to django-d...@googlegroups.com. > To unsubscribe from this group, send email to django-develop...@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/django-developers?hl=en. > > -- |
Re: Feature proposal: Q.push(...) and QuerySet.filter(relfield=Q(...)) | Javier Guerra Giraldez | 10/3/11 11:01 AM | On Fri, Sep 30, 2011 at 4:37 PM, Johannes Dollinger i really want to have this feature! so i went and did a quick [1]:https://code.djangoproject.com/ticket/16979 really untested, but a variation that doesn't modify core seems to -- |
Re: Feature proposal: Q.push(...) and QuerySet.filter(relfield=Q(...)) | Johannes Dollinger | 10/4/11 2:35 AM | Thanks for starting this! As an afterthought: The cleanest solution would probably be an implementation in Q.__init__. __ |
Re: Feature proposal: Q.push(...) and QuerySet.filter(relfield=Q(...)) | Javier Guerra Giraldez | 10/4/11 5:02 AM | On Tue, Oct 4, 2011 at 4:35 AM, Johannes Dollinger because it's applied recursively not just to tree.Node, but to the > While I'm not sold on the method name (perhaps prefix() or shift() or even __rshift__() would be clearer), I believe the ability to modify Q objects outside of filter()-Expressions is important. __rshift__() is too C++ for my taste :-) but I see the motivation. > Just like &, |, and ~, adding a prefix is an operation on Q objects. And the API should provide a way to manipulate them fully outside of QuerySets. yes, that sounds cleaner. i'm still not sure if that should be on Q -- |
Re: Feature proposal: Q.push(...) and QuerySet.filter(relfield=Q(...)) | Javier Guerra Giraldez | 10/4/11 7:36 AM | On Tue, Oct 4, 2011 at 4:35 AM, Johannes Dollinger > as you could write Q(foo=Q(...)) instead of Q(..).push('foo') I thought that would work magically, since the Q object would just I've just refactored as a Node._prefix() method that calls a -- |