Loading django/db/models/sql/compiler.py +1 −1 Original line number Diff line number Diff line Loading @@ -555,7 +555,7 @@ class SQLCompiler(object): # If we get to this point and the field is a relation to another model, # append the default ordering for that model unless the attribute name # of the field is specified. if field.is_relation and path and opts.ordering and name != field.attname: if field.is_relation and opts.ordering and getattr(field, 'attname', None) != name: # Firstly, avoid infinite loops. if not already_seen: already_seen = set() Loading docs/releases/1.9.2.txt +3 −0 Original line number Diff line number Diff line Loading @@ -41,3 +41,6 @@ Bugfixes * Fixed CSRF cookie check on POST requests when ``USE_X_FORWARDED_PORT=True`` (:ticket:`26094`). * Fixed a ``QuerySet.order_by()`` crash when ordering by a relational field of a ``ManyToManyField`` ``through`` model (:ticket:`26092`). tests/m2m_through/models.py +1 −0 Original line number Diff line number Diff line Loading @@ -65,6 +65,7 @@ class CustomMembership(models.Model): class Meta: db_table = "test_table" ordering = ["date_joined"] class TestNoDefaultsOrNulls(models.Model): Loading tests/m2m_through/tests.py +14 −0 Original line number Diff line number Diff line Loading @@ -198,6 +198,20 @@ class M2mThroughTests(TestCase): attrgetter("name") ) def test_order_by_relational_field_through_model(self): CustomMembership.objects.create(person=self.jim, group=self.rock) CustomMembership.objects.create(person=self.bob, group=self.rock) CustomMembership.objects.create(person=self.jane, group=self.roll) CustomMembership.objects.create(person=self.jim, group=self.roll) self.assertQuerysetEqual( self.rock.custom_members.order_by('custom_person_related_name'), [self.jim, self.bob], lambda x: x ) self.assertQuerysetEqual( self.roll.custom_members.order_by('custom_person_related_name'), [self.jane, self.jim], lambda x: x ) def test_query_first_model_by_intermediate_model_attribute(self): Membership.objects.create( person=self.jane, group=self.roll, Loading Loading
django/db/models/sql/compiler.py +1 −1 Original line number Diff line number Diff line Loading @@ -555,7 +555,7 @@ class SQLCompiler(object): # If we get to this point and the field is a relation to another model, # append the default ordering for that model unless the attribute name # of the field is specified. if field.is_relation and path and opts.ordering and name != field.attname: if field.is_relation and opts.ordering and getattr(field, 'attname', None) != name: # Firstly, avoid infinite loops. if not already_seen: already_seen = set() Loading
docs/releases/1.9.2.txt +3 −0 Original line number Diff line number Diff line Loading @@ -41,3 +41,6 @@ Bugfixes * Fixed CSRF cookie check on POST requests when ``USE_X_FORWARDED_PORT=True`` (:ticket:`26094`). * Fixed a ``QuerySet.order_by()`` crash when ordering by a relational field of a ``ManyToManyField`` ``through`` model (:ticket:`26092`).
tests/m2m_through/models.py +1 −0 Original line number Diff line number Diff line Loading @@ -65,6 +65,7 @@ class CustomMembership(models.Model): class Meta: db_table = "test_table" ordering = ["date_joined"] class TestNoDefaultsOrNulls(models.Model): Loading
tests/m2m_through/tests.py +14 −0 Original line number Diff line number Diff line Loading @@ -198,6 +198,20 @@ class M2mThroughTests(TestCase): attrgetter("name") ) def test_order_by_relational_field_through_model(self): CustomMembership.objects.create(person=self.jim, group=self.rock) CustomMembership.objects.create(person=self.bob, group=self.rock) CustomMembership.objects.create(person=self.jane, group=self.roll) CustomMembership.objects.create(person=self.jim, group=self.roll) self.assertQuerysetEqual( self.rock.custom_members.order_by('custom_person_related_name'), [self.jim, self.bob], lambda x: x ) self.assertQuerysetEqual( self.roll.custom_members.order_by('custom_person_related_name'), [self.jane, self.jim], lambda x: x ) def test_query_first_model_by_intermediate_model_attribute(self): Membership.objects.create( person=self.jane, group=self.roll, Loading