[Django] #31481: Using model functions in model meta ordering fails in admin

36 views
Skip to first unread message

Django

unread,
Apr 19, 2020, 4:31:41 PM4/19/20
to django-...@googlegroups.com
#31481: Using model functions in model meta ordering fails in admin
-----------------------------------------+------------------------
Reporter: GergelyKalmar | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 3.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
Let's assume we have two models as follows:

{{{
from django.db import models


class ExampleObject(models.Model):
some_field = models.CharField(max_length=50)

class Meta:
ordering = [models.functions.Lower('some_field')]


class ExampleExtension(models.Model):
example_object = models.OneToOneField(
ExampleObject, primary_key=True, on_delete=models.PROTECT
)
}}}

If I register the latter model in admin:
{{{
from django.contrib import admin

from myapp import models

admin.site.register(models.ExampleExtension)
}}}

I get the following error:
{{{
FieldError at /admin/myapp/exampleextension/
Cannot resolve keyword 'some_field' into field. Choices are:
example_object, example_object_id
}}}

If I remove the `Lower` model function it works as expected. Also, the
ordering generally seems to work fine in all other places, except in the
admin interface. Am I doing something silly?

--
Ticket URL: <https://code.djangoproject.com/ticket/31481>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 20, 2020, 12:56:36 AM4/20/20
to django-...@googlegroups.com
#31481: Admin crash when using functions in model meta ordering of o2o primary key.
--------------------------------+--------------------------------------
Reporter: Gergely Kalmár | Owner: nobody
Type: Bug | Status: closed
Component: contrib.admin | Version: master
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------
Changes (by felixxm):

* status: new => closed
* version: 3.0 => master
* resolution: => fixed


Comment:

Thanks for this ticket. It was fixed in
013147fae2b9168b06c495aa78c10725cab294cd (I'm not marking this as a
duplicate because it's a different scenario).

--
Ticket URL: <https://code.djangoproject.com/ticket/31481#comment:1>

Django

unread,
Apr 20, 2020, 5:09:12 AM4/20/20
to django-...@googlegroups.com
#31481: Admin crash when using functions in model meta ordering of o2o primary key.
--------------------------------+--------------------------------------
Reporter: Gergely Kalmár | Owner: nobody
Type: Bug | Status: closed
Component: contrib.admin | Version: master
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------

Comment (by Gergely Kalmár):

Thank you! Am I right that the patch is not released yet? Is there a
workaround (besides patching Django locally) in the meantime?

--
Ticket URL: <https://code.djangoproject.com/ticket/31481#comment:2>

Django

unread,
Apr 20, 2020, 5:26:48 AM4/20/20
to django-...@googlegroups.com
#31481: Admin crash when using functions in model meta ordering of o2o primary key.
--------------------------------+--------------------------------------
Reporter: Gergely Kalmár | Owner: nobody
Type: Bug | Status: closed
Component: contrib.admin | Version: master
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------

Comment (by felixxm):

> Am I right that the patch is not released yet?

Yes, it's not released. It will be included in Django 3.1.

> Is there a workaround (besides patching Django locally) in the meantime?

You can move `ordering` to the `ExampleExtension`, e.g.
{{{
class ExampleExtension(models.Model):
...

class Meta:
ordering = [models.functions.Lower('example_object__some_field')]
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/31481#comment:3>

Django

unread,
May 22, 2020, 10:43:01 AM5/22/20
to django-...@googlegroups.com
#31481: Admin crash when using functions in model meta ordering of o2o primary key.
--------------------------------+--------------------------------------
Reporter: Gergely Kalmár | Owner: nobody
Type: Bug | Status: closed
Component: contrib.admin | Version: master
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------

Comment (by Gergely Kalmár):

Hm, it seems that Django 3.1 is coming only around August - would that be
possible to move this small patch into a patch release on 3.0 instead? The
workaround doesn't really work that nicely because I need the ordering to
apply also to ExampleObject instances (e.g. for listing) and not only when
using them through ExampleExtension (in this hypothetical case). This
issue keeps breaking the admin interface and it would take quite some work
to work around it in all places I would have to :(.

--
Ticket URL: <https://code.djangoproject.com/ticket/31481#comment:4>

Reply all
Reply to author
Forward
0 new messages