* cc: David Sanders (added)
Comment:
fwiw I can't reproduce the issue on 4.1.1 🤔 …
Here's what I tried (full tracebacks omitted for brevity):
{{{
class Parent(Model):
...
class Child(Model):
parent = ForeignKey(Parent, on_delete=CASCADE)
Child.objects.all().order_by('asdf')
FieldError: Cannot resolve keyword 'asdf' into field. Choices are: id,
parent, parent_id
Child.objects.all().order_by('parent__id')
<QuerySet []>
Child.objects.all().order_by('parent__asdf')
FieldError: Unsupported lookup 'asdf' for BigAutoField or join on the
field not permitted, perhaps you meant df?
During handling of the above exception, another exception occurred:
FieldError: Cannot resolve keyword 'asdf' into field. Choices are: child,
id
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/34012#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Comment (by Klaas van Schelven):
Good catch, this only happens when an explicit ordering is defined on the
`Parent`'s `Meta`:
{{{
from django.db import models
class Parent(models.Model):
class Meta:
ordering = ["id"]
class Child(models.Model):
parent = models.ForeignKey(Parent, on_delete=models.CASCADE)
}}}
Reproducable up to Django 4.1.1
--
Ticket URL: <https://code.djangoproject.com/ticket/34012#comment:3>
Comment (by David Sanders):
felixx this looks like an issue with overriding the foreign model's
ordering.
If I repeat the test with Klaas' models and print the query it reveals it
still attempts to use `parent.id`:
{{{
class Parent(Model):
class Meta:
ordering = ["id"]
class Child(Model):
parent = ForeignKey(Parent, on_delete=CASCADE)
print(Child.objects.all().order_by('parent__asdf').query)
SELECT "sample_child"."id", "sample_child"."parent_id" FROM "sample_child"
INNER JOIN "sample_parent" ON ("sample_child"."parent_id" =
"sample_parent"."id") ORDER BY "sample_parent"."id" ASC
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/34012#comment:4>
* owner: nobody => David Sanders
* status: new => assigned
* has_patch: 0 => 1
Comment:
PR: https://github.com/django/django/pull/16070
--
Ticket URL: <https://code.djangoproject.com/ticket/34012#comment:5>
* version: 4.0 => 4.1
--
Ticket URL: <https://code.djangoproject.com/ticket/34012#comment:6>