用两个__英寸的django orm过滤器
我有两个字段的模型。 product_ids_list = [1,2,3,4] Selling_prices_list = [65,89,93] product_ids_list和alling_prices_list的长度相同。
我能够以一个product_id进行下面的ORM过滤器,并且类似于相应的销售价格。
product_instances = Product.objects.filter(product_id=product_ids_list[0], selling_price=selling_prices_list[0]).first()
但是,如何使用product_ids_list进行一个db调用,并且它是对应的selling_prices_list。
product_instances = Product.objects.filter(product_id__in=product_ids_list, selling_price__in=selling_prices_list).first() (This isn't working in the expected way)
I'm having model with two field.
product_ids_list = [1,2,3,4]
selling_prices_list = [65, 89, 93]
And length of product_ids_list and selling_prices_list are same.
I'm able to perform below ORM filter for one product_id and it's corresponding selling price like this.
product_instances = Product.objects.filter(product_id=product_ids_list[0], selling_price=selling_prices_list[0]).first()
But how to do perform ORM filter with just one DB call with product_ids_list and it's corresponding selling_prices_list.
product_instances = Product.objects.filter(product_id__in=product_ids_list, selling_price__in=selling_prices_list).first() (This isn't working in the expected way)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 product_id 是 foreferkey 和 selling_price 是 integerfield ,则过滤代码将是:
当您提供ID列表时,我们必须比较产品ID。如果我们使用 product_id__in 过滤,则将产品对象与列表中的ID进行比较。
建议: 最好将外国基金名称作为 product 而不是 product_id 避免混乱像上面的ORM过滤器一样。
If product_id is ForeignKey and selling_price is IntegerField then the filter code would be:
As you are supplying list of IDs, we would have to compare product IDs. If we filter with product_id__in, it compares Product object with IDs in the list.
Suggesstion: It would be better to have ForeignKey field name as product instead of product_id to avoid confusion like the above ORM filter.