Opened 10 years ago

Closed 10 years ago

#22696 closed New feature (wontfix)

Support for DISTINCT ON in sqlite3

Reported by: jakob.hedman@… Owned by: nobody
Component: Database layer (models, ORM) Version: dev
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


.distinct() raises NotImplementedError when using sqlite3 backend. Maybe DISTINCT ON could be implemented like this?

Change History (2)

comment:1 by mardini, 10 years ago

I'm personally not a fan of introducing such hack to the ORM. IMHO, sticking to the native features of the backends will keep the codebase cleaner and more robust. So since SQLite3 doesn't support DISTINCT ON fields, I don't think implementing this workaround is a good idea.

Having said that, if you need a distinct on field query, you can implement it through the QuerySet API, using a combination of values().distinct() or values().annotate(), here's a simple example:


This is roughly equivalent to using distinct on (first_name) with an order by last_name.

If you still think this is a good idea, you could get more attention and discussion by using Django developers mailing list (


Last edited 10 years ago by mardini (previous) (diff)

comment:2 by Claude Paroz, 10 years ago

Resolution: wontfix
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top