summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsarahboyce <sarahvboyce95@gmail.com>2023-03-18 10:18:11 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-03-28 09:26:39 +0200
commit45ecd9acca9b36093e274f47b6877a5f79108d9e (patch)
tree5b6b40aef6105d648476736f50464644f6c421cd
parentd687febce5868545f99974d2499a91f81a32fef5 (diff)
Fixed #28384 -- Fixed ModelAdmin.lookup_allowed() for OneToOneField primary keys and nested relations.
-rw-r--r--django/contrib/admin/options.py10
-rw-r--r--tests/modeladmin/tests.py29
2 files changed, 35 insertions, 4 deletions
diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
index 5240b07cd6..49c816dc9e 100644
--- a/django/contrib/admin/options.py
+++ b/django/contrib/admin/options.py
@@ -461,12 +461,14 @@ class BaseModelAdmin(metaclass=forms.MediaDefiningClass):
# 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
+ 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):
diff --git a/tests/modeladmin/tests.py b/tests/modeladmin/tests.py
index c6b4a565c8..5604e39746 100644
--- a/tests/modeladmin/tests.py
+++ b/tests/modeladmin/tests.py
@@ -154,6 +154,35 @@ class ModelAdminTests(TestCase):
ma.lookup_allowed("employee__department__code", "test_value"), True
)
+ @isolate_apps("modeladmin")
+ def test_lookup_allowed_foreign_primary(self):
+ class Country(models.Model):
+ name = models.CharField(max_length=256)
+
+ class Place(models.Model):
+ country = models.ForeignKey(Country, models.CASCADE)
+
+ class Restaurant(models.Model):
+ place = models.OneToOneField(Place, models.CASCADE, primary_key=True)
+
+ class Waiter(models.Model):
+ restaurant = models.ForeignKey(Restaurant, models.CASCADE)
+
+ class WaiterAdmin(ModelAdmin):
+ list_filter = [
+ "restaurant__place__country",
+ "restaurant__place__country__name",
+ ]
+
+ ma = WaiterAdmin(Waiter, self.site)
+ self.assertIs(ma.lookup_allowed("restaurant__place__country", "1"), True)
+ self.assertIs(
+ ma.lookup_allowed("restaurant__place__country__id__exact", "1"), True
+ )
+ self.assertIs(
+ ma.lookup_allowed("restaurant__place__country__name", "test_value"), True
+ )
+
def test_field_arguments(self):
# If fields is specified, fieldsets_add and fieldsets_change should
# just stick the fields into a formsets structure and return it.