1
 

More like this

Comments

fylb (on February 16, 2010):

When ordering on multiple columns, you may want to use this:

result = map( lambda e: e + ' NULLS LAST', result)

instead of

result[-1] += ' NULLS LAST'

#

ludi182 (on July 12, 2010):

Hei - why im getting this error?

[Mon Jul 12 17:55:38 2010] [error] [client 10.8.1.2] mod_wsgi (pid=7660): Exception occurred processing WSGI script '/home/python-environments/test/apache/e_n_test.wsgi'.
[...]
[Mon Jul 12 17:55:40 2010] [error] [client 10.8.1.2]   File "[...]/models.py", line 11, in get_query_set
[Mon Jul 12 17:55:40 2010] [error] [client 10.8.1.2]     return QuerySet(self.model)
[Mon Jul 12 17:55:40 2010] [error] [client 10.8.1.2]   File "[...]/query_fix.py", line 26, in __init__
[Mon Jul 12 17:55:40 2010] [error] [client 10.8.1.2]     self.query = query or Query(self.model, connection)
[Mon Jul 12 17:55:40 2010] [error] [client 10.8.1.2]   File "[...]/query_fix.py", line 9, in __init__
[Mon Jul 12 17:55:40 2010] [error] [client 10.8.1.2]     super(Query, self).__init__(*args, **kwargs)
[Mon Jul 12 17:55:40 2010] [error] [client 10.8.1.2]   File "/usr/lib/python2.5/site-packages/django/db/models/sql/query.py", line 127, in __init__
[Mon Jul 12 17:55:40 2010] [error] [client 10.8.1.2]     self.where = where()
[Mon Jul 12 17:55:40 2010] [error] [client 10.8.1.2] TypeError: Error when calling the metaclass bases
[Mon Jul 12 17:55:40 2010] [error] [client 10.8.1.2]     'DatabaseWrapper' object is not callable

hope u have an idea

  • django 1.2
  • python 2.5
  • no nulls_last call yet
  • i just copy-pasted your code..

#

fylb (on August 10, 2010):

For django 1.2, small patch to apply:

 class QuerySet(QuerySet):
-    def __init__(self, model=None, query=None):
-        super(QuerySet, self).__init__(model=model, query=query)
-        self.query = query or Query(self.model, connection)
+    def __init__(self, model=None, query=None, using=None):
+        super(QuerySet, self).__init__(model=model, query=query, using=using)
+        self.query = query or Query(self.model)

#

fylb (on August 11, 2010):

Don't mind my previous comment, this just fixes the error but does not order by NULLS LAST.

Workaround: after building your queryset, do:

queryset.query.add_extra(None, None, None, None, None, ('table.field NULLS LAST',))

the dot in table.field is important because in django.db.models.sql.compiler, there is:

    for field in ordering:
        # ....
        if '.' in field:
            # This came in through an extra(order_by=...) addition. Pass it
            # on verbatim.
            table, col = col.split('.', 1)
            if (table, col) not in processed_pairs:
                elt = '%s.%s' % (qn(table), col)
                processed_pairs.add((table, col))
                if not distinct or elt in select_aliases:
                    result.append('%s %s' % (elt, order))
                    group_by.append((elt, []))

bit of a hack...

#

(Forgotten your password?)