Skip to content

Instantly share code, notes, and snippets.

@diox
Created April 11, 2011 11:00
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