簡體   English   中英

如何獲取標簽為列表Django-taggit中標簽的子集的所有對象

[英]How to get all the objects whose tags are subset of tags in list Django-taggit

我正在使用django-taggit進行標記。

class Taggedwebsites(TaggedItemBase):
  content_object = models.ForeignKey('website')

class website(models.Model):
      tags=TaggableManager(through=Taggedwebsites,blank=True)

現在,我希望所有其標簽是標簽列表超集的網站都可以動態提供。

例如,

tag_list=['python','django','database']

然后,我要所有必須至少具有這三個集合的網站對象。

result=website.objects.filter(tags__name_on=tag_list).distinct()

不起作用,因為它不提供其標簽是tag_list的超集的對象。

如何在過濾器中執行此查詢?

在查詢“ tags__name_on”中,“ on”一詞未在django中定義,應改為使用in。 如果您希望對象的對象必須至少具有這三個集合,則應使用“ in”參數。 這是querysets的文檔:docs.djangoproject.com/en/1.8/ref/models/querysets/#in

嘗試:

result=website.objects.filter(tags__name__in=tag_list).distinct()

要么:

result=website.objects.distinct(tags__name__in=tag_list)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM