如果我的问题已经存在,我会事先道歉,但我还没有找到。
有一个模型:
class Artikul_cabinets(models.Model):
artikul_cabinets = models.CharField(verbose_name="Артикул шкафа", max_length=20)
title_cabinets = models.CharField(verbose_name="Описание шкафа", max_length=200)
width_cabinets = models.ManyToManyField(Width_cabinets)
depth_cabinets = models.ManyToManyField(Depth_cabinets)
unit_cabinets = models.ManyToManyField(Unit_cabinets)
weight_cabinets = models.ManyToManyField(Weight_cabinets)
type_cabinets = models.ForeignKey(Type_cabinets, default=1)
color_cabinets = models.ForeignKey(Color_cabinets)
glass_cabinets = models.ManyToManyField(Glass_cabinets)
class Meta:
verbose_name_plural = "Артикул шкафа"
def __str__(self):
return self.artikul_cabinets
有必要在字段上进行选择
glass_cabinets = models.ManyToManyField(Glass_cabinets)
选择如下:
data = Artikul_cabinets.objects.filter(Q(glass_cabinets=perf) &
Q(glass_cabinets=glass)
perf
和glass
具有不同值的变量。
然后我返回到我的空QuerySet,尽管记录中存在参数'perf'和'glass'的数据库元素。
告诉我我做错了什么。
也尝试过:
data = Artikul_cabinets.objects.filter(Q(glass_cabinets=perf),
Q(glass_cabinets=glass)
并且也没有用,但是如果你把运算符'|'条件或工作正确。
答案 0 :(得分:1)
所以我认为你应该做Artikul_cabinets.objects.filter(glass_cabinets=perf).filter(glass_cabinets=glass)
检查How to filter model results for multiple values for a many to many field in django