在django中使用变量列表过滤查询

时间:2022-03-24 01:38:19

I have a three table model like that :

我有一个三表模型:

在django中使用变量列表过滤查询

I want to filter results of a query with a variable list of items for exemple :

我想用例如项的变量列表来过滤查询结果:

listTags = ["landscape","green"]
results = ListTag.objects.filter(tag__name__in=listTags).select_related()

But the result of that query is all the ListTag objects with landscape OR green but what i want it's a list of ListTag objects with landscape AND green

但该查询的结果是具有横向OR绿色的所有ListTag对象,但我想要的是具有横向和绿色的ListTag对象列表

I saw a lot a of answers about that problem but a lot of them use a static list of tags, what i want it's to filter with a variable listtags list

我看到了很多关于这个问题的答案,但是他们中的很多人都使用静态的标签列表,我希望它用变量listtags列表进行过滤

Edit : the model

编辑:模型

class Picture(models.Model):
    title = models.CharField(max_length=50,null=True, blank=False, verbose_name=('name'))

    def __str__(self):
        return self.titre

class Tag(models.Model):
    name = models.CharField(max_length=50,null=True, blank=False, verbose_name=('name'))

    def __str__(self):
        return self.name

class ListTags(models.Model):
    picture = models.ForeignKey(Picture, blank=False, on_delete=models.CASCADE, related_name='picture')
    tag = models.ForeignKey(Tag, blank=False, on_delete=models.CASCADE, related_name='tag')

2 个解决方案

#1


1  

You can try to use Django Q object.

您可以尝试使用Django Q对象。

In your case this could be:

在你的情况下,这可能是:

from django.db.models import Q

...

listTags = ["landscape","green"]

query = Q()
for tag in listTags:
    query &= Q(tag__name = tag)

results = ListTag.objects.filter(query).select_related()

addition:
if you want just pictures with tags, then you could use many-to-many relationships. But if you want use tags for different types of models, then u need to use generic relations.

另外:如果您只想要带有标签的图片,那么您可以使用多对多关系。但是如果你想为不同类型的模型使用标签,那么你需要使用通用关系。

In first case models structure could be:

在第一种情况下,模型结构可以是:

from django.db import models

class Tag(models.Model):
    name = models.CharField(max_length=50, null=True, blank=False, verbose_name=('name'))

    def __str__(self):
        return self.name

class Picture(models.Model):
    title = models.CharField(max_length=50, null=True, blank=False, verbose_name=('name'))
    tags = models.ManyToManyField(Tag)

    def __str__(self):
        return self.title

With m2m relation Q object will not work, so to get all pictures with landscape and green tags you can use filter chaining:

使用m2m关系Q对象将无法正常工作,因此要使用横向和绿色标签获取所有图片,您可以使用过滤器链接:

listTags = ["landscape", "green"]

results = models.Picture.objects.all()
for tag in listTags:
    results = results.filter(tags__name = tag)

#2


0  

I believe the code below would make this an AND query rather than an OR query:

我相信下面的代码会使这个AND查询而不是OR查询:

listTags = ["landscape","green"]

filters = {}
for value in listTags:
    filters['tag__name__in'] = value

results = ListTag.objects.filter(**filters)

#1


1  

You can try to use Django Q object.

您可以尝试使用Django Q对象。

In your case this could be:

在你的情况下,这可能是:

from django.db.models import Q

...

listTags = ["landscape","green"]

query = Q()
for tag in listTags:
    query &= Q(tag__name = tag)

results = ListTag.objects.filter(query).select_related()

addition:
if you want just pictures with tags, then you could use many-to-many relationships. But if you want use tags for different types of models, then u need to use generic relations.

另外:如果您只想要带有标签的图片,那么您可以使用多对多关系。但是如果你想为不同类型的模型使用标签,那么你需要使用通用关系。

In first case models structure could be:

在第一种情况下,模型结构可以是:

from django.db import models

class Tag(models.Model):
    name = models.CharField(max_length=50, null=True, blank=False, verbose_name=('name'))

    def __str__(self):
        return self.name

class Picture(models.Model):
    title = models.CharField(max_length=50, null=True, blank=False, verbose_name=('name'))
    tags = models.ManyToManyField(Tag)

    def __str__(self):
        return self.title

With m2m relation Q object will not work, so to get all pictures with landscape and green tags you can use filter chaining:

使用m2m关系Q对象将无法正常工作,因此要使用横向和绿色标签获取所有图片,您可以使用过滤器链接:

listTags = ["landscape", "green"]

results = models.Picture.objects.all()
for tag in listTags:
    results = results.filter(tags__name = tag)

#2


0  

I believe the code below would make this an AND query rather than an OR query:

我相信下面的代码会使这个AND查询而不是OR查询:

listTags = ["landscape","green"]

filters = {}
for value in listTags:
    filters['tag__name__in'] = value

results = ListTag.objects.filter(**filters)