diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index 0281fa84d6..019bfe9789 100644 --- a/django/contrib/admin/options.py +++ b/django/contrib/admin/options.py @@ -266,7 +266,7 @@ class ModelAdmin(BaseModelAdmin): if self.exclude is None: exclude = [] else: - exclude = self.exclude + exclude = list(self.exclude) defaults = { "form": self.form, "fields": fields, @@ -780,7 +780,7 @@ class InlineModelAdmin(BaseModelAdmin): if self.exclude is None: exclude = [] else: - exclude = self.exclude + exclude = list(self.exclude) defaults = { "form": self.form, "formset": self.formset, diff --git a/django/forms/models.py b/django/forms/models.py index 6024a1c6c8..dad3483455 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -528,6 +528,7 @@ def inlineformset_factory(parent_model, model, form=ModelForm, if fk.unique: max_num = 1 if exclude is not None: + exclude = list(exclude) exclude.append(fk.name) else: exclude = [fk.name] diff --git a/tests/regressiontests/inline_formsets/models.py b/tests/regressiontests/inline_formsets/models.py index c00703852f..8b6f5dd342 100644 --- a/tests/regressiontests/inline_formsets/models.py +++ b/tests/regressiontests/inline_formsets/models.py @@ -51,5 +51,7 @@ Traceback (most recent call last): Exception: has no field named 'test' +# Regression test for #9171. +>>> ifs = inlineformset_factory(Parent, Child, exclude=('school',), fk_name='mother') """ } diff --git a/tests/regressiontests/modeladmin/models.py b/tests/regressiontests/modeladmin/models.py index f7526ab3dd..3a7d3f031f 100644 --- a/tests/regressiontests/modeladmin/models.py +++ b/tests/regressiontests/modeladmin/models.py @@ -123,6 +123,14 @@ displayed because you forgot to add it to fields/fielsets >>> ma = BandAdmin(Band, site) >>> ma.get_form(request).base_fields.keys() ['name', 'sign_date'] + +# You can also pass a tuple to `exclude`. + +>>> class BandAdmin(ModelAdmin): +... exclude = ('bio',) +>>> ma = BandAdmin(Band, site) +>>> ma.get_form(request).base_fields.keys() +['name', 'sign_date'] # Using `fields` and `exclude`.