Hi Daniel,

On Sun, 2006-07-16 at 09:07 +0000, [EMAIL PROTECTED] wrote:
> Hi.
> 
> This is probably trivial, but I've not managed to find the solution.
> How do I filter objects based on the count of a ManyToManyField?
> 
> My example:
> 
> I have two classes -- Blog and Submission:
> 
> class Blog( models.Model ):
>     entries = models.ManyToManyField( Submission )
> 
> class Submission( models.Model ):
>   [... whatever ]
> 
> I want to fetch a list of all Blog instances which have at least one
> Submission , i.e. entries.count() > 0. Changing the model is not an
> option.
> 
> I've been trying all kinds of permutations of filter, entries, count,
> gt, etc, such as:
> Blog.objects.filter(entries__count__gt = 0)

I'll agree this one isn't too obvious (until you see the answer):

        Blog.objects.filter(entries__isnull = False)
        
will do what you want (have a look at the underlying SQL query is you
are interested in seeing that it really works as advertised).

Regards,
Malcolm


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to