Opened 5 years ago

Closed 5 years ago

Last modified 4 years ago

#31481 closed Bug (fixed)

Admin crash when using functions in model meta ordering of o2o primary key.

Reported by: Gergely Kalmár Owned by: nobody
Component: contrib.admin Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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?

Change History (5)

comment:1 by Mariusz Felisiak, 5 years ago

Resolution: fixed
Status: newclosed
Summary: Using model functions in model meta ordering fails in adminAdmin crash when using functions in model meta ordering of o2o primary key.
Version: 3.0master

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

comment:2 by Gergely Kalmár, 5 years ago

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

comment:3 by Mariusz Felisiak, 5 years ago

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')]

comment:4 by Gergely Kalmár, 5 years ago

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 :(.

in reply to:  4 comment:5 by Mariusz Felisiak, 4 years ago

Replying to 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?

No. This is not a regression in Django 3.0. Per our backporting policy this means it doesn't qualify for a backport to 3.0.x. See https://docs.djangoproject.com/en/3.0/internals/release-process/ for more details.

Note: See TracTickets for help on using tickets.
Back to Top