Opened 13 months ago

Closed 13 months ago

Last modified 13 months ago

#35029 closed Bug (duplicate)

DisallowedModelAdminLookup for uuid field

Reported by: James Lao Owned by: nobody
Component: contrib.admin Version: 5.0
Severity: Normal Keywords: DisallowedModelAdminLookup, uuid
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by James Lao)

On Django 5.0 and MariaDB 11.0.4, 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

Change History (4)

comment:1 by James Lao, 13 months ago

Description: modified (diff)

comment:2 by James Lao, 13 months ago

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

comment:3 by James Lao, 13 months ago

Resolution: fixed
Status: newclosed

Seems to be the same as #35020

comment:4 by David Sanders, 13 months ago

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