Wow - that's *exactly* the way I ended up doing it!

It took me a full working day to figure it out though. I'll post up a
snippet when I'm at work tomorrow. Basically the same thing except
filtering with pk and not name- I looped through the exact matches and
then excluded the rest.



On Sep 15, 8:29 am, Alec Shaner <asha...@chumpland.org> wrote:
> 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='Exac 
> t2').exclude(category__in=bad_categories)
>
>
>
> On Tue, Sep 14, 2010 at 5:58 PM, Jason <goodri...@gmail.com> 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 django-us...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@google 
> > groups.com>
> > .
> > 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 django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to