hi i have one dynamically generated list lets we assume
my_list = ['11','13','15']
my model.py is
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)
my list is refer to option_value in OptionProduct table
now i want to get that product in which all option values are include
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
so it want to get product_name 1 and 2 as a output because it have all three value of my_list in option_value every time my_list is change so it work according to that
so how can i make query set in django
I hope this can be possible in two steps. try below query.
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)]
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.