From 30a389bd7795016d7f48bcda997e5dea5116f9bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Pinheiro=20Marques=20de=20Ara=C3=BAjo?= Date: Wed, 13 Dec 2017 04:39:27 -0300 Subject: [PATCH] Fixed #28898 -- Corrected admin check to allow a OneToOneField in ModelAdmin.autocomplete_fields. --- AUTHORS | 1 + django/contrib/admin/checks.py | 2 +- docs/releases/2.0.1.txt | 3 +++ tests/modeladmin/test_checks.py | 15 ++++++++++++++- 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/AUTHORS b/AUTHORS index df5bbb82ab0..a46cf625cb2 100644 --- a/AUTHORS +++ b/AUTHORS @@ -689,6 +689,7 @@ answer newbie questions, and generally made Django that much better: Robert Wittams Rob Hudson Robin Munn + Rodrigo Pinheiro Marques de Araújo Romain Garrigues Ronny Haryanto Ross Poulton diff --git a/django/contrib/admin/checks.py b/django/contrib/admin/checks.py index c53224499a6..26c5c3eef6a 100644 --- a/django/contrib/admin/checks.py +++ b/django/contrib/admin/checks.py @@ -104,7 +104,7 @@ class BaseModelAdminChecks: except FieldDoesNotExist: return refer_to_missing_field(field=field_name, option=label, model=model, obj=obj, id='admin.E037') else: - if not (field.many_to_many or field.many_to_one): + if not field.many_to_many and not isinstance(field, models.ForeignKey): return must_be( 'a foreign key or a many-to-many field', option=label, obj=obj, id='admin.E038' diff --git a/docs/releases/2.0.1.txt b/docs/releases/2.0.1.txt index 25ba39cffe9..c956d7ce6b2 100644 --- a/docs/releases/2.0.1.txt +++ b/docs/releases/2.0.1.txt @@ -23,3 +23,6 @@ Bugfixes (:ticket:`28856`). * Reallowed filtering a queryset with ``GeometryField=None`` (:ticket:`28896`). + +* Corrected admin check to allow a ``OneToOneField`` in + ``ModelAdmin.autocomplete_fields`` (:ticket:`28898`). diff --git a/tests/modeladmin/test_checks.py b/tests/modeladmin/test_checks.py index eaca153bd8d..5559a8fe3ca 100644 --- a/tests/modeladmin/test_checks.py +++ b/tests/modeladmin/test_checks.py @@ -6,7 +6,9 @@ from django.core.checks import Error from django.forms.models import BaseModelFormSet from django.test import SimpleTestCase -from .models import Band, Song, ValidationTestInlineModel, ValidationTestModel +from .models import ( + Band, Song, User, ValidationTestInlineModel, ValidationTestModel, +) class CheckTestCase(SimpleTestCase): @@ -1243,3 +1245,14 @@ class AutocompleteFieldsTests(CheckTestCase): site = AdminSite() site.register(Band, SearchFieldsAdmin) self.assertIsValid(AutocompleteAdmin, Song, admin_site=site) + + def test_autocomplete_is_onetoone(self): + class UserAdmin(ModelAdmin): + search_fields = ('name', ) + + class Admin(ModelAdmin): + autocomplete_fields = ('best_friend', ) + + site = AdminSite() + site.register(User, UserAdmin) + self.assertIsValid(Admin, ValidationTestModel, admin_site=site)