1

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

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    Try `Sale.objects.filter(product__id__contains=[1,2])` – Ash Singh Apr 04 '22 at 15:55
  • Does this answer your question? [How to do many-to-many Django query to find book with 2 given authors?](https://stackoverflow.com/questions/5301996/how-to-do-many-to-many-django-query-to-find-book-with-2-given-authors) – Brian Destura Apr 04 '22 at 23:23

1 Answers1

0
Sale.objects.filter(product__id__contains=[1,2])
lord stock
  • 1,191
  • 5
  • 32