views:

16

answers:

1

Hello djangoists.

How to I make the following possible?

models.py

class Article(models.Model):
    #...
    regions = models.ManyToManyField(Region)

elsewhere...

regions = Region.objects.all()
articles = Article.objects.filter(regions=regions)

Currently, the 'articles' retrieved are only from a match with the first region in the queryset, i.e. regions[0].

Of course I would like to get article matches from 1-n regions found.

Kind thanks.

Daryl.

+4  A: 

Hi Daryl, maybe this can help:

http://docs.djangoproject.com/en/1.2/ref/models/querysets/#s-in

With that in mind, you could rewrite your code like this:

regions = Region.objects.all()
articles = Article.objects.filter(regions_in=regions)

And it should work all right.

Federico Cáceres
Right on! And I added .distinct() for a little more fidelity. Thx Fed'
Daryl
+1. Came here to post exactly that.
Manoj Govindan