Using django filter() with a querset as args or kwargs
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)
Cu开发者_运维百科rrently, 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.
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.
精彩评论