Per Django 1.9 (e possibilmente 1.8) è possibile utilizzare questo:
from django.db import connections, models
from django.db.models.sql.compiler import SQLCompiler
class NullsLastSQLCompiler(SQLCompiler):
def get_order_by(self):
result = super().get_order_by()
if result and self.connection.vendor == 'postgresql':
return [(expr, (sql + ' NULLS LAST', params, is_ref))
for (expr, (sql, params, is_ref)) in result]
return result
class NullsLastQuery(models.sql.query.Query):
"""Use a custom compiler to inject 'NULLS LAST' (for PostgreSQL)."""
def get_compiler(self, using=None, connection=None):
if using is None and connection is None:
raise ValueError("Need either using or connection")
if using:
connection = connections[using]
return NullsLastSQLCompiler(self, connection, using)
class NullsLastQuerySet(models.QuerySet):
def __init__(self, model=None, query=None, using=None, hints=None):
super().__init__(model, query, using, hints)
self.query = query or NullsLastQuery(self.model)
E poi al modello (s):
objects = NullsLastQuerySet.as_manager()
Questo è basato sulla risposta di Tim in https://stackoverflow.com/a/17077587/15690.
È stato riaperto il ticket per aggiungere supporto a questo servizio su Django: .
Grazie. Non è davvero la soluzione perfetta, ma un po 'funziona – GabiMe
Prego. Non penso che ci sia una soluzione perfetta per questo, o almeno uno che è direttamente contemplato da Django, dato [questo gruppo di google post] (https://groups.google.com/d/msg/django-users/sxbpqmkRyCU/ qUYjX3niCOEJ) di Malcolm Treddinick. – Mariano