Skip to content

Instantly share code, notes, and snippets.

@diox
Created April 11, 2011 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diox/913360 to your computer and use it in GitHub Desktop.
Save diox/913360 to your computer and use it in GitHub Desktop.
from django.db import models
class Foo(models.Model):
bar = models.PositiveSmallIntegerField(default=1)
class Meta:
ordering = ['bar']
class Child(Foo):
barchild = models.PositiveSmallIntegerField(default=1)
>>> str(Child.objects.order_by('pk').query)
'SELECT "test_pk_foo"."id", "test_pk_foo"."bar", "test_pk_child"."foo_ptr_id", "test_pk_child"."barchild" FROM "test_pk_child" INNER JOIN "test_pk_foo" ON ("test_pk_child"."foo_ptr_id" = "test_pk_foo"."id") ORDER BY "test_pk_foo"."bar" ASC'
# order_by('pk') doesn't do anything, the default ordering is used instead! (Tested with
# sqlite3 and postgresql_psycopg2 engines)
>>> str(Child.objects.order_by('id').query)
'SELECT "test_pk_foo"."id", "test_pk_foo"."bar", "test_pk_child"."foo_ptr_id", "test_pk_child"."barchild" FROM "test_pk_child" INNER JOIN "test_pk_foo" ON ("test_pk_child"."foo_ptr_id" = "test_pk_foo"."id") ORDER BY "test_pk_child"."foo_ptr_id" ASC'
# order_by('id') works.
# Note that removing the default ordering in the parent Model also fixes the bug.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment