Opened 7 years ago
Last modified 7 years ago
#28484 closed Uncategorized
QuerySet.extra keyword — at Initial Version
Reported by: | Antonio Carrasco | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.11 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Related to the #extra deprecation https://docs.djangoproject.com/en/1.11/ref/models/querysets/#extra
Given this example:
queryset = Model.objects.all()
ordered_ids = "1,3,5,7"
queryset.annotate(custom=RawSQL(f"SELECT FIELD(id, %s)", (ordered_ids,))).order_by("custom")
queryset.extra(select={"custom": f"FIELD(id, {ordered_ids})"}, order_by=[custom]).distinct()
The result of both queries is different. But executing the .query.str() query in the Mysql native console, the result is the same.
The result with #extra is ok (the same the native mysql console returns) but the result with annotate is wrong.