3 回答

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超9個(gè)贊
摘要:
正如jpic和sgallen在評(píng)論中所建議的那樣,一個(gè)選項(xiàng)是.filter()
為每個(gè)類別添加。每個(gè)附加項(xiàng)都會(huì)filter
添加更多聯(lián)接,這對(duì)于一小組類別來(lái)說(shuō)應(yīng)該不是問(wèn)題。
有聚合 方法。對(duì)于大量類別,此查詢將更短并且可能更快。
您還可以選擇使用自定義查詢。
一些例子
測(cè)試設(shè)置:
class Photo(models.Model): tags = models.ManyToManyField('Tag')class Tag(models.Model): name = models.CharField(max_length=50) def __unicode__(self): return self.nameIn [2]: t1 = Tag.objects.create(name='holiday')In [3]: t2 = Tag.objects.create(name='summer')In [4]: p = Photo.objects.create()In [5]: p.tags.add(t1)In [6]: p.tags.add(t2)In [7]: p.tags.all()Out[7]: [<Tag: holiday>, <Tag: summer>]
使用鏈?zhǔn)竭^(guò)濾器方法:
In [8]: Photo.objects.filter(tags=t1).filter(tags=t2)Out[8]: [<Photo: Photo object>]
結(jié)果查詢:
In [17]: print Photo.objects.filter(tags=t1).filter(tags=t2).query SELECT "test_photo"."id"FROM "test_photo"INNER JOIN "test_photo_tags" ON ("test_photo"."id" = "test_photo_tags"."photo_id")INNER JOIN "test_photo_tags" T4 ON ("test_photo"."id" = T4."photo_id")WHERE ("test_photo_tags"."tag_id" = 3 AND T4."tag_id" = 4 )
請(qǐng)注意,每個(gè)都會(huì)為查詢filter
添加更多內(nèi)容JOINS
。
使用注釋 方法:
In [29]: from django.db.models import CountIn [30]: Photo.objects.filter(tags__in=[t1, t2]).annotate(num_tags=Count('tags')).filter(num_tags=2)Out[30]: [<Photo: Photo object>]
結(jié)果查詢:
In [32]: print Photo.objects.filter(tags__in=[t1, t2]).annotate(num_tags=Count('tags')).filter(num_tags=2).query SELECT "test_photo"."id", COUNT("test_photo_tags"."tag_id") AS "num_tags"FROM "test_photo"LEFT OUTER JOIN "test_photo_tags" ON ("test_photo"."id" = "test_photo_tags"."photo_id")WHERE ("test_photo_tags"."tag_id" IN (3, 4))GROUP BY "test_photo"."id", "test_photo"."id"HAVING COUNT("test_photo_tags"."tag_id") = 2
AND
ed Q
對(duì)象不起作用:
In [9]: from django.db.models import QIn [10]: Photo.objects.filter(Q(tags__name='holiday') & Q(tags__name='summer'))Out[10]: []In [11]: from operator import and_In [12]: Photo.objects.filter(reduce(and_, [Q(tags__name='holiday'), Q(tags__name='summer')]))Out[12]: []
結(jié)果查詢:
In [25]: print Photo.objects.filter(Q(tags__name='holiday') & Q(tags__name='summer')).query SELECT "test_photo"."id"FROM "test_photo"INNER JOIN "test_photo_tags" ON ("test_photo"."id" = "test_photo_tags"."photo_id")INNER JOIN "test_tag" ON ("test_photo_tags"."tag_id" = "test_tag"."id")WHERE ("test_tag"."name" = holiday AND "test_tag"."name" = summer )

TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超10個(gè)贊
另一種有效的方法,雖然只有PostgreSQL,但它正在使用django.contrib.postgres.fields.ArrayField:
從docs復(fù)制的示例:
>>> Post.objects.create(name='First post', tags=['thoughts', 'django'])
>>> Post.objects.create(name='Second post', tags=['thoughts'])
>>> Post.objects.create(name='Third post', tags=['tutorial', 'django'])
>>> Post.objects.filter(tags__contains=['thoughts'])
<QuerySet [<Post: First post>, <Post: Second post>]>
>>> Post.objects.filter(tags__contains=['django'])
<QuerySet [<Post: First post>, <Post: Third post>]>
>>> Post.objects.filter(tags__contains=['django', 'thoughts'])
<QuerySet [<Post: First post>]>
ArrayField有一些更強(qiáng)大的功能,如重疊和索引轉(zhuǎn)換。

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超4個(gè)贊
這也可以通過(guò)使用Django ORM和一些Python魔法的動(dòng)態(tài)查詢生成來(lái)完成:)
from operator import and_from django.db.models import Q categories = ['holiday', 'summer']res = Photo.filter(reduce(and_, [Q(tags__name=c) for c in categories]))
我們的想法是為每個(gè)類別生成適當(dāng)?shù)腝對(duì)象,然后使用AND運(yùn)算符將它們組合到一個(gè)QuerySet中。例如,你的例子就等于
res = Photo.filter(Q(tags__name='holiday') & Q(tags__name='summer'))
添加回答
舉報(bào)