'How to perform AND condition on many to many field
I have two models
class Sale(AuthorTimeStampedModel):
product = models.ManyToManyField(
Product, related_name='sale_product', null=True, blank=True)
def __str__(self):
return f"{self.id}"
class Product(AuthorTimeStampedModel):
id = models.IntegerField()
Now I want to apply query set to get all sales which have product_id 1 and 2
I am able to find Q function but it will result or condition but I want and condition
Solution 1:[1]
Sale.objects.filter(product__id__contains=[1,2])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
