From 65c283be163212f5679b5edbd6a24c88b3af04ce Mon Sep 17 00:00:00 2001 From: Antoine Cheneau Date: Wed, 1 Nov 2023 11:31:32 +0100 Subject: [PATCH] Fixed #34927 -- Fixed admin system check for inlines with foreign keys to proxy models. Follow up to 0e8be73812a6e62d5a6b12a585d133b56bc2bf52. --- AUTHORS | 1 + django/forms/models.py | 2 ++ tests/modeladmin/test_checks.py | 39 +++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/AUTHORS b/AUTHORS index 2a331e4818..0247503197 100644 --- a/AUTHORS +++ b/AUTHORS @@ -93,6 +93,7 @@ answer newbie questions, and generally made Django that much better: ant9000@netwise.it Anthony Briggs Anthony Wright + Antoine Chéneau Anton Samarchyan Antoni Aloy Antonio Cavedoni diff --git a/django/forms/models.py b/django/forms/models.py index ff4b83fe59..6a2608c0b3 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -1211,6 +1211,7 @@ def _get_foreign_key(parent_model, model, fk_name=None, can_fail=False): if len(fks_to_parent) == 1: fk = fks_to_parent[0] parent_list = parent_model._meta.get_parent_list() + parent_list.append(parent_model) if ( not isinstance(fk, ForeignKey) or ( @@ -1236,6 +1237,7 @@ def _get_foreign_key(parent_model, model, fk_name=None, can_fail=False): else: # Try to discover what the ForeignKey from model to parent_model is parent_list = parent_model._meta.get_parent_list() + parent_list.append(parent_model) fks_to_parent = [ f for f in opts.fields diff --git a/tests/modeladmin/test_checks.py b/tests/modeladmin/test_checks.py index 2ed27f8a3d..47b1b40ed7 100644 --- a/tests/modeladmin/test_checks.py +++ b/tests/modeladmin/test_checks.py @@ -1268,6 +1268,45 @@ class FkNameCheckTests(CheckTestCase): self.assertIsValid(TestModelAdmin, ValidationTestModel) + def test_proxy_model(self): + class Reporter(Model): + pass + + class ProxyJournalist(Reporter): + class Meta: + proxy = True + + class Article(Model): + reporter = ForeignKey(ProxyJournalist, on_delete=CASCADE) + + class ArticleInline(admin.TabularInline): + model = Article + + class ReporterAdmin(admin.ModelAdmin): + inlines = [ArticleInline] + + self.assertIsValid(ReporterAdmin, Reporter) + + def test_proxy_model_fk_name(self): + class ReporterFkName(Model): + pass + + class ProxyJournalistFkName(ReporterFkName): + class Meta: + proxy = True + + class ArticleFkName(Model): + reporter = ForeignKey(ProxyJournalistFkName, on_delete=CASCADE) + + class ArticleInline(admin.TabularInline): + model = ArticleFkName + fk_name = "reporter" + + class ReporterAdmin(admin.ModelAdmin): + inlines = [ArticleInline] + + self.assertIsValid(ReporterAdmin, ReporterFkName) + def test_proxy_model_parent(self): class Parent(Model): pass