Try this:

I'm assuming you define Article.category as a ManyToMany field? I also
assumed for the example that Category has a name field.

# Build a queryset of all categories not in desired set, e.g., 'Exact1' and
'Exact2'
bad_categories = Category.objects.exclude(category_name__in=['Exact1',
'Exact2'])
# Filter your exact categories, then exclude the bad ones
Article.objects.filter(category__name='Exact1').filter(category__name='Exact2').exclude(category__in=bad_categories)


On Tue, Sep 14, 2010 at 5:58 PM, Jason <[email protected]> wrote:

> Say for example you have two models:
>
> Article
> Category
>
>
> Articles can have multiple categories.
>
> How would you go about finding the Articles that contain only a
> certain set of Categories?
>
> The 'in' operator doesn't do me any good. Excludes look like they are
> needed...
>
>
> I'm in a situation where quickly sorting by SETS of categories is
> needed and I'm having a tough time. My database programming skills are
> pretty weak.
>
> I'm thinking of making an intermediate model called CategorySets.
>
> Articles would then have a relationship to a CategorySet and I can
> sort very easily on this.
>
>
> (note my actual project isn't using Articles but it's quicker to
> explain in these terms).
>
> Does anyone have any tips or experience here?
>
> --
> 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]<django-users%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>

-- 
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?hl=en.

Reply via email to