浏览代码

Fixed #34927 -- Fixed admin system check for inlines with foreign keys to proxy models.

Follow up to 0e8be73812a6e62d5a6b12a585d133b56bc2bf52.
Antoine Cheneau 1 年之前
父节点
当前提交
65c283be16
共有 3 个文件被更改,包括 42 次插入0 次删除
  1. 1 0
      AUTHORS
  2. 2 0
      django/forms/models.py
  3. 39 0
      tests/modeladmin/test_checks.py

+ 1 - 0
AUTHORS

@@ -93,6 +93,7 @@ answer newbie questions, and generally made Django that much better:
     ant9000@netwise.it
     Anthony Briggs <anthony.briggs@gmail.com>
     Anthony Wright <ryow.college@gmail.com>
+    Antoine Chéneau <antoine.cheneau@outlook.com>
     Anton Samarchyan <desecho@gmail.com>
     Antoni Aloy
     Antonio Cavedoni <http://cavedoni.com/>

+ 2 - 0
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

+ 39 - 0
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