Good afternoon, there is a queryset, you need to remove duplicates from it:

example = models.Object.objects.values('name', 'photo__name').distinct()

we have a join, as a result we need to get that the name and photo__name do not have the same values, how to implement it using Django ORM? Thank you!

1 answer 1

There is a solution to the forehead

 example = models.Object.objects.values('name', 'photo__name').extra( where=['"name" != "photo__name"']).distinct() 

which generates something like the following SQL

 SELECT "object"."name", "photo"."photo__name" FROM "object" INNER JOIN [...] WHERE ("name" != "photo__name") ``` 

perhaps because of the join, you will have to specify the full names of the tables, for example

 where=['"object"."name" != "photo"."photo__name"']