簡體   English   中英

在這種情況下,如何在Django中設置queryset

[英]How can i make queryset in django for this scenario

嗨,我有一個動態生成的列表,讓我們假設

my_list = ['11','13','15']

我的model.py是

class OptionProduct(models.Model):

    product_name = models.ForeignKey('products.Product', null=True, blank=True, verbose_name=_('Product'))
    option_name = models.ForeignKey(Options,null=True, blank=True)
    option_value = models.ForeignKey(OptionValue,null=True, blank=True)

我的列表是引用OptionProduct表中的option_value

現在我想獲得包含所有選項值的產品

product_name |   option_value | option_name

1           |       11      |   1

1           |       13      |   1

2           |       11      |   1

2           |       13      |   1

3           |       11      |   1

3           |       13      |   1

2           |       15      |   2

1           |       15      |   2

所以它想獲得product_name 1和2作為輸出,因為每次my_list更改時,它在option_value中都具有my_list的所有三個值,因此它根據

所以我該如何在Django中設置查詢

我希望這可以通過兩個步驟實現。 請嘗試以下查詢。

import collections
option_product_list = OptionProduct.objects.filter(option_value__id__in=my_list).values_list('product_name', flat=True)
output = [item for item, count in collections.Counter(list(option_product_list)).items() if count >= len(my_list)]

暫無
暫無

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

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