[Django] #33678: Admin crash when sorting by a foreign key that's sorted with functions

3 views
Skip to first unread message

Django

unread,
May 3, 2022, 8:11:46 PM5/3/22
to django-...@googlegroups.com
#33678: Admin crash when sorting by a foreign key that's sorted with functions
-----------------------------------------+------------------------
Reporter: Eduardo Rivas | Owner: nobody
Type: Uncategorized | Status: new
Component: contrib.admin | Version: 4.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 |
-----------------------------------------+------------------------
Possibly related to https://code.djangoproject.com/ticket/31481

{{{
# models.py
from django.db import models
from django.db.models.functions import Lower


class Material(models.Model):
code = models.CharField(max_length=255, unique=True)

class Meta:
ordering = (Lower("code"),)

def __str__(self):
return self.code


class QuotePartMaterial(models.Model):
material = models.ForeignKey(Material, on_delete=models.PROTECT)

def __str__(self):
return str(self.material)
}}}


{{{
# admin.py
from django.contrib import admin
from . import models


admin.site.register(models.Material)


@admin.register(models.QuotePartMaterial)
class QuotePartMaterialAdmin(admin.ModelAdmin):
list_display = ("material",)
}}}

Loading the admin works fine, but If I try to manually sort
QuotePartMaterial records in the admin by clicking the "Material" column
header, I get:

{{{
FieldError at /admin/example/quotepartmaterial/
Cannot resolve keyword 'code' into field. Choices are: id, material,
material_id
}}}

Removing the Lower() call on Material.Meta.ordering makes the issue go
away.

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

Reply all
Reply to author
Forward
0 new messages