[Answered ]-Strange behavior of ORM in Django for ManyToMany field

1👍

From the Django Docs

By default, a QuerySet will not eliminate duplicate rows. In practice, this is rarely a problem, because simple queries such as Blog.objects.all() don’t introduce the possibility of duplicate result rows. However, if your query spans multiple tables, it’s possible to get duplicate results when a QuerySet is evaluated. That’s when you’d use distinct().

You need to apply the distinct() method, So you may need to do :

print Offer.objects.all().filter(shop__city=1).distinct()
👤Amyth

1👍

Since you are placing a join on 3 table. Its going to return you the result with the offers for all the shops in that city. Try to do this

Offer.objects.all().filter(shop__city=1).distinct()

0👍

Try this,

 Offer.objects.filter(shop__city__id = 1).distinct()

Leave a comment