| 47 | * A couple of changes to `order_by()`: |
| 48 | * Previously, it you passed no arguments to `order_by()`, it did nothing. Now, it clears the ordering (previously there was no way to remove any existing ordering). So if you are expecting the "do nothing" behaviour, make sure to check the arguments you are passing to `order_by()` and don't call it if you have no arguments. |
| 49 | * `order_by()` will raise an error if you try to order by a related model and resolving that model's standard ordering leads to an infinite loop (ordering by a related model tacks the target model's default ordering onto the end). This commonly comes up when ordering by a `ForeignKey` to "self". The previous code wouldn't raise an error here, but the results weren't particularly intuitive. However, if you still want the old behaviour, specify the ordering constraint explicitly so that it ends in a non-related field. For the "reference to self" case, you could make this change: |
| 50 | {{{ |
| 51 | #!python |
| 52 | class Tag(models.Model): |
| 53 | parent = models.ForeignKey(self, blank=True, null=True) |
| 54 | |
| 55 | class Meta: |
| 56 | # ordering = ('parent',) # Old code now causes an infinite loop. |
| 57 | ordering = ('parent__id',) # Last field is a non-relation. No problem. |
| 58 | }}} |