mirror of
				https://github.com/django/django.git
				synced 2025-10-30 17:16:10 +00:00 
			
		
		
		
	[3.1.x] Fixed #31657 -- Fixed ordering by attnames of self-referential ForeignKeys.
Backport of 037a624120 from master
			
			
This commit is contained in:
		
				
					committed by
					
						 Mariusz Felisiak
						Mariusz Felisiak
					
				
			
			
				
	
			
			
			
						parent
						
							bdfad57319
						
					
				
				
					commit
					4385ef0119
				
			| @@ -720,7 +720,12 @@ class SQLCompiler: | |||||||
|         # If we get to this point and the field is a relation to another model, |         # If we get to this point and the field is a relation to another model, | ||||||
|         # append the default ordering for that model unless it is the pk |         # append the default ordering for that model unless it is the pk | ||||||
|         # shortcut or the attribute name of the field that is specified. |         # shortcut or the attribute name of the field that is specified. | ||||||
|         if field.is_relation and opts.ordering and getattr(field, 'attname', None) != name and name != 'pk': |         if ( | ||||||
|  |             field.is_relation and | ||||||
|  |             opts.ordering and | ||||||
|  |             getattr(field, 'attname', None) != pieces[-1] and | ||||||
|  |             name != 'pk' | ||||||
|  |         ): | ||||||
|             # Firstly, avoid infinite loops. |             # Firstly, avoid infinite loops. | ||||||
|             already_seen = already_seen or set() |             already_seen = already_seen or set() | ||||||
|             join_tuple = tuple(getattr(self.query.alias_map[j], 'join_cols', None) for j in joins) |             join_tuple = tuple(getattr(self.query.alias_map[j], 'join_cols', None) for j in joins) | ||||||
|   | |||||||
| @@ -353,6 +353,11 @@ class OrderingTests(TestCase): | |||||||
|             ['Article 2', 'Article 1'], |             ['Article 2', 'Article 1'], | ||||||
|             attrgetter('headline'), |             attrgetter('headline'), | ||||||
|         ) |         ) | ||||||
|  |         self.assertQuerysetEqual( | ||||||
|  |             Article.objects.filter(author__isnull=False).order_by('author__editor_id'), | ||||||
|  |             ['Article 1', 'Article 2'], | ||||||
|  |             attrgetter('headline'), | ||||||
|  |         ) | ||||||
|  |  | ||||||
|     def test_order_by_f_expression(self): |     def test_order_by_f_expression(self): | ||||||
|         self.assertQuerysetEqual( |         self.assertQuerysetEqual( | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user