[Django] #35029: DisallowedModelAdminLookup for uuid field

24 views
Skip to first unread message

Django

unread,
Dec 11, 2023, 4:07:44 PM12/11/23
to django-...@googlegroups.com
#35029: DisallowedModelAdminLookup for uuid field
-------------------------------------+-------------------------------------
Reporter: jameslao | Owner: nobody
Type: Bug | Status: new
Component: | Version: 5.0
contrib.admin | Keywords:
Severity: Normal | DisallowedModelAdminLookup, uuid
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
In Django 5.0, if we create a model with primary ID field with UUIDField,
and another model referencing it, then in Django admin, if we create a
filter of the second model with the first model, an error of
DisallowedModelAdminLookup will be thrown.

in models.py

{{{
from django.db import models
import uuid

# Create your models here.

class Request(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4,
editable=False, verbose_name='Request ID')
name = models.CharField(max_length=50, blank=True)


class RequestItem(models.Model):
request = models.ForeignKey(Request, on_delete=models.CASCADE,
related_name='items')
description = models.CharField(max_length=255, blank=True)
}}}

in admin.py


{{{
from . import models

@admin.register(models.RequestItem)
class RequestItemAdmin(admin.ModelAdmin):
list_display = (
'request',
)
list_filter = (
'request',
)


@admin.register(models.Request)
class RequestAdmin(admin.ModelAdmin):
list_display = (
'id', 'name'
)
list_filter = (
'id',
)

}}}

Then the filter of request in RequestItemAdmin will throw an error

{{{
DisallowedModelAdminLookup at /admin/uid/requestitem/
Filtering by request__id__exact not allowed
}}}

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

Django

unread,
Dec 11, 2023, 4:13:16 PM12/11/23
to django-...@googlegroups.com
#35029: DisallowedModelAdminLookup for uuid field
-------------------------------------+-------------------------------------
Reporter: jameslao | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage:
DisallowedModelAdminLookup, uuid | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by jameslao:

Old description:

New description:

On Django 5.0 and MariaDB 11.0.4, if we create a model with primary ID

in models.py

in admin.py


{{{
from . import models

}}}

--

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

Django

unread,
Dec 11, 2023, 5:36:00 PM12/11/23
to django-...@googlegroups.com
#35029: DisallowedModelAdminLookup for uuid field
-------------------------------------+-------------------------------------
Reporter: James Lao | Owner: nobody

Type: Bug | Status: new
Component: contrib.admin | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage:
DisallowedModelAdminLookup, uuid | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by James Lao):

Seems like this issue is not limited to UUID but to any field other than
AutoField used as id. Maybe we should revert this change?

The difference is in django/contrib/admin/options.py => BaseModelAdmin =>
lookup_allowed

in 4.2.8

{{{
for part in lookup.split(LOOKUP_SEP):
try:
field = model._meta.get_field(part)
except FieldDoesNotExist:
# Lookups on nonexistent fields are ok, since they're
ignored
# later.
break
# It is allowed to filter on values that would be found from
local
# model anyways. For example, if you filter on
employee__department__id,
# then the id value would be found already from
employee__department_id.
if not prev_field or (
prev_field.is_relation
and field not in prev_field.path_infos[-1].target_fields
):
relation_parts.append(part)
if not getattr(field, "path_infos", None):
# This is not a relational field, so further parts
# must be transforms.
break
prev_field = field
model = field.path_infos[-1].to_opts.model
}}}

In 5.0.0


{{{
for part in lookup.split(LOOKUP_SEP):
try:
field = model._meta.get_field(part)
except FieldDoesNotExist:
# Lookups on nonexistent fields are ok, since they're
ignored
# later.
break
if not prev_field or (
prev_field.is_relation
and field not in model._meta.parents.values()
and field is not model._meta.auto_field
and (
model._meta.auto_field is None
or part not in getattr(prev_field, "to_fields", [])
)
):
relation_parts.append(part)
if not getattr(field, "path_infos", None):
# This is not a relational field, so further parts
# must be transforms.
break
prev_field = field
model = field.path_infos[-1].to_opts.model
}}}

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

Django

unread,
Dec 11, 2023, 5:38:31 PM12/11/23
to django-...@googlegroups.com
#35029: DisallowedModelAdminLookup for uuid field
-------------------------------------+-------------------------------------
Reporter: James Lao | Owner: nobody
Type: Bug | Status: closed
Component: contrib.admin | Version: 5.0
Severity: Normal | Resolution: fixed

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

* status: new => closed
* resolution: => fixed


Comment:

Seems to be the same as #35020

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

Django

unread,
Dec 11, 2023, 7:37:35 PM12/11/23
to django-...@googlegroups.com
#35029: DisallowedModelAdminLookup for uuid field
-------------------------------------+-------------------------------------
Reporter: James Lao | Owner: nobody
Type: Bug | Status: closed
Component: contrib.admin | Version: 5.0
Severity: Normal | Resolution: duplicate

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

* resolution: fixed => duplicate


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

Reply all
Reply to author
Forward
0 new messages