开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜