mirror of https://github.com/django/django.git
Fixed #35020 -- Fixed ModelAdmin.lookup_allowed() for non-autofield primary keys.
Thanks Joshua Goodwin for the report.
Regression in 45ecd9acca
.
This commit is contained in:
parent
00ef74376e
commit
f80669d2f5
|
@ -474,24 +474,24 @@ class BaseModelAdmin(metaclass=forms.MediaDefiningClass):
|
||||||
# Lookups on nonexistent fields are ok, since they're ignored
|
# Lookups on nonexistent fields are ok, since they're ignored
|
||||||
# later.
|
# later.
|
||||||
break
|
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):
|
if not getattr(field, "path_infos", None):
|
||||||
# This is not a relational field, so further parts
|
# This is not a relational field, so further parts
|
||||||
# must be transforms.
|
# must be transforms.
|
||||||
break
|
break
|
||||||
|
if (
|
||||||
|
not prev_field
|
||||||
|
or (field.is_relation and field not in model._meta.parents.values())
|
||||||
|
or (
|
||||||
|
prev_field.is_relation
|
||||||
|
and model._meta.auto_field is None
|
||||||
|
and part not in getattr(prev_field, "to_fields", [])
|
||||||
|
)
|
||||||
|
):
|
||||||
|
relation_parts.append(part)
|
||||||
prev_field = field
|
prev_field = field
|
||||||
model = field.path_infos[-1].to_opts.model
|
model = field.path_infos[-1].to_opts.model
|
||||||
|
|
||||||
if len(relation_parts) <= 1:
|
if not relation_parts:
|
||||||
# Either a local field filter, or no fields at all.
|
# Either a local field filter, or no fields at all.
|
||||||
return True
|
return True
|
||||||
valid_lookups = {self.date_hierarchy}
|
valid_lookups = {self.date_hierarchy}
|
||||||
|
|
|
@ -9,4 +9,6 @@ Django 5.0.1 fixes several bugs in 5.0.
|
||||||
Bugfixes
|
Bugfixes
|
||||||
========
|
========
|
||||||
|
|
||||||
* ...
|
* Reallowed, following a regression in Django 5.0, using a foreign key to a
|
||||||
|
model with a primary key that is not ``AutoField`` in
|
||||||
|
:attr:`.ModelAdmin.list_filter` (:ticket:`35020`).
|
||||||
|
|
|
@ -162,6 +162,20 @@ class ModelAdminTests(TestCase):
|
||||||
True,
|
True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@isolate_apps("modeladmin")
|
||||||
|
def test_lookup_allowed_non_autofield_primary_key(self):
|
||||||
|
class Country(models.Model):
|
||||||
|
id = models.CharField(max_length=2, primary_key=True)
|
||||||
|
|
||||||
|
class Place(models.Model):
|
||||||
|
country = models.ForeignKey(Country, models.CASCADE)
|
||||||
|
|
||||||
|
class PlaceAdmin(ModelAdmin):
|
||||||
|
list_filter = ["country"]
|
||||||
|
|
||||||
|
ma = PlaceAdmin(Place, self.site)
|
||||||
|
self.assertIs(ma.lookup_allowed("country__id__exact", "DE", request), True)
|
||||||
|
|
||||||
@isolate_apps("modeladmin")
|
@isolate_apps("modeladmin")
|
||||||
def test_lookup_allowed_foreign_primary(self):
|
def test_lookup_allowed_foreign_primary(self):
|
||||||
class Country(models.Model):
|
class Country(models.Model):
|
||||||
|
|
Loading…
Reference in New Issue