【发布时间】:2020-07-08 17:05:14
【问题描述】:
我有一个由三个不同模型给出的查询集:
class Sottocategoria(models.Model):
name = models.CharField('Nome del sottoprodotto', max_length=30)
class A(models.Model):
codice_commessa=models.ForeignKey()
prodotto=models.ForeignKey()
sottocategoria=models.ForeignKey(Sottocategoria)
class B(models.Model):
quantity=models.ForeignKey()
price=models.DecimalField()
sottocategoria=models.ForeignKey(Sottocategoria)
现在我设置了以下 for 循环:
for sottocategoria_id, totale in
(B.objects.values_list('sottocategoria__id').annotate(totale=(Sum(F('quantity') * F('price')))):
....
我需要过滤模型 B 中的 sottocategoria__id,它们存在于模型 A 中。
广告示例如果我在 model A sottocategoria 中等于 {'abc','abcd','abcdf'} 并且模型 B sottocategoria 等于 {'abc','abcd','abcdf', '1234'},则在我的 for 循环中我只想过滤 {'abc','abcd','abcdf'}。
【问题讨论】:
-
'abc'等是什么?名字? -
是的,是 Sottocateoria 模型的名称
标签: python python-3.x django django-models django-views