Hi all,

I've been redirected to the mailing list from the ticked I opened [0]. 
Therein, I proposed to add a new method to the Q object API for prefixing Q 
objects with a related field's name in order to be able to create reusable 
Q objects.

I think the use case I pointed out might have been misunderstood. Such an 
API would allow writing a manager like this one, let's say for a 
Subscription model:

class SubscriptionManager(Manager):
    def active_q(self):
        return Q(cancelled=False) & Q(end_date__lt=datetime.datetime.now())

    # This one is just for convenience and compatibility with the evolved 
practice
    def active(self):
        return self.filter(self.active_q())

We could then perform the following lookups:

# All active subscriptions, nothing special here
Subscription.objects.active()
# Given that a user has a ManyToManyField subscriptions, get all users with 
active subscriptions, without repeating any business logic
User.objects.filter(Subscription.objects.active_q().prefix("subscriptions"))

The traditional approach would be to implement methods for filtering the 
queryset on both the managers for Subscription and User and thus 
duplicating logic, or use sub-queries like so:

User.objects.filter(subscriptions__in=Subscription.objects.active())

which is clearly not wanted because of performance.

What are your opinions?

Best regards
Robert

[0] https://code.djangoproject.com/ticket/30631


-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/30020900-7a4a-442e-9cce-af3d75a27c15%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
  • ... 'Robert Schindler' via Django developers (Contributions to Django itself)
    • ... Matthew Pava
      • ... 'Robert Schindler' via Django developers (Contributions to Django itself)
        • ... 'Robert Schindler' via Django developers (Contributions to Django itself)
    • ... Shai Berger
      • ... 'Robert Schindler' via Django developers (Contributions to Django itself)
        • ... 'Robert Schindler' via Django developers (Contributions to Django itself)

Reply via email to