[Fixed]-Get objects which have NO relation to the current object

1👍

You can get the count of subscriptions per customer, then then .filter the customers without subscriptions. Something like this should do the trick:

unsubscribed = Customer.objects.annotate(
        subscription_count=models.Count('subscription')
    ).filter(subscription_count=0)

Another way (which should be faster) is to use the __isnull notation against the reverse relation of the FK. Here is how:

 unsubscribed = Customer.objects.filter(subscription__isnull=True)
👤Todor

Leave a comment