How to exclude duplicates using a subquery with Django and MySQL
Consider the following example:
Let’s say you want to query the latest books of each author, so every author is only listed once in the queryset. There are various ways of going about this (for example, if you are using PostgreSQL you can use DISTINCT ON), but for a Django 2.2 project where I’m using MySQL I tried to use the following Subquery expression:
However, executing this query raises an exception if you use MySQL 5.7 (haven’t tested on later versions):
The problem is that you cannot use id__in
in combination with a sliced subquery .values('id')[:1])
. You can workaround this by instead of excluding the “duplicates”, to annotate the latest book of that author and filtering on that value with an F() expression: