From f4647179ccd13c852563205273163b641d4b01a5 Mon Sep 17 00:00:00 2001 From: Shubham singh Date: Tue, 17 Dec 2019 02:46:39 +0530 Subject: [PATCH] Fixed #31086 -- Improved error message for admin model inlines with more than one foreign key to the same parent. --- django/forms/models.py | 3 ++- docs/ref/checks.txt | 3 ++- tests/admin_checks/tests.py | 4 +++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/django/forms/models.py b/django/forms/models.py index d72c62f7f0f..0684199db54 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -1032,7 +1032,8 @@ def _get_foreign_key(parent_model, model, fk_name=None, can_fail=False): ) else: raise ValueError( - "'%s' has more than one ForeignKey to '%s'." % ( + "'%s' has more than one ForeignKey to '%s'. You must specify " + "a 'fk_name' attribute." % ( model._meta.label, parent_model._meta.label, ) diff --git a/docs/ref/checks.txt b/docs/ref/checks.txt index 9c5b86eafd8..fcbfec98cb1 100644 --- a/docs/ref/checks.txt +++ b/docs/ref/checks.txt @@ -645,7 +645,8 @@ inline on a :class:`~django.contrib.admin.ModelAdmin`. * **admin.E201**: Cannot exclude the field ````, because it is the foreign key to the parent model ``.``. * **admin.E202**: ```` has no ``ForeignKey`` to ````./ - ```` has more than one ``ForeignKey`` to ````. + ```` has more than one ``ForeignKey`` to ````. You must + specify a ``fk_name`` attribute. * **admin.E203**: The value of ``extra`` must be an integer. * **admin.E204**: The value of ``max_num`` must be an integer. * **admin.E205**: The value of ``min_num`` must be an integer. diff --git a/tests/admin_checks/tests.py b/tests/admin_checks/tests.py index 1829294a471..2a9643cd95d 100644 --- a/tests/admin_checks/tests.py +++ b/tests/admin_checks/tests.py @@ -636,7 +636,9 @@ class SystemChecksTestCase(SimpleTestCase): errors = MyAdmin(Album, AdminSite()).check() expected = [ checks.Error( - "'admin_checks.TwoAlbumFKAndAnE' has more than one ForeignKey to 'admin_checks.Album'.", + "'admin_checks.TwoAlbumFKAndAnE' has more than one ForeignKey " + "to 'admin_checks.Album'. You must specify a 'fk_name' " + "attribute.", obj=TwoAlbumFKAndAnEInline, id='admin.E202', )