简体   繁体   中英

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

I am using django-taggit for tagging.

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

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

Now I want all the website whose tags are superset of tags list provided dynamically.

For example,

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

Then I want all the website objects which must have at least these three sets.

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

doesn't works as it doesn't gives object whose tags are superset of tag_list.

How to perform this query in filter?

In the query "tags__name_on" the 'on' word is not define in django, you should use in instead. You want the objects with objects which must have at least these three sets, you should use the 'in' parameter. Here is the documentation for the querysets : docs.djangoproject.com/en/1.8/ref/models/querysets/#in

try:

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

or:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM