Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug #17428: Admin formfield validation uses form model instead of registered model. #528

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions django/contrib/admin/validation.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.db import models from django.db import models
from django.db.models.fields import FieldDoesNotExist from django.db.models.fields import FieldDoesNotExist
from django.forms.models import (BaseModelForm, BaseModelFormSet, fields_for_model, from django.forms.models import (BaseModelForm, BaseModelFormSet, fields_for_model,
_get_foreign_key) _get_foreign_key, modelform_factory)
from django.contrib.admin import ListFilter, FieldListFilter from django.contrib.admin import ListFilter, FieldListFilter
from django.contrib.admin.util import get_fields_from_path, NotRelationField from django.contrib.admin.util import get_fields_from_path, NotRelationField
from django.contrib.admin.options import (flatten_fieldsets, BaseModelAdmin, from django.contrib.admin.options import (flatten_fieldsets, BaseModelAdmin,
Expand Down Expand Up @@ -381,19 +381,12 @@ def get_field(cls, model, opts, label, field):
% (cls.__name__, label, field, model._meta.app_label, model.__name__)) % (cls.__name__, label, field, model._meta.app_label, model.__name__))


def check_formfield(cls, model, opts, label, field): def check_formfield(cls, model, opts, label, field):
if getattr(cls.form, 'base_fields', None): form = modelform_factory(model, form=cls.form)
try: try:
cls.form.base_fields[field] form.base_fields[field]
except KeyError: except KeyError:
raise ImproperlyConfigured("'%s.%s' refers to field '%s' that " raise ImproperlyConfigured("'%s.%s' refers to field '%s' that "
"is missing from the form." % (cls.__name__, label, field)) "is missing from the form." % (cls.__name__, label, field))
else:
fields = fields_for_model(model)
try:
fields[field]
except KeyError:
raise ImproperlyConfigured("'%s.%s' refers to field '%s' that "
"is missing from the form." % (cls.__name__, label, field))


def fetch_attr(cls, model, opts, label, field): def fetch_attr(cls, model, opts, label, field):
try: try:
Expand Down
24 changes: 23 additions & 1 deletion tests/regressiontests/admin_validation/tests.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase from django.test import TestCase


from .models import Song, Book, Album, TwoAlbumFKAndAnE, State, City from .models import Song, Book, Album, TwoAlbumFKAndAnE, State, City, Author




class SongForm(forms.ModelForm): class SongForm(forms.ModelForm):
Expand Down Expand Up @@ -281,3 +281,25 @@ class FieldsOnFormOnlyAdmin(admin.ModelAdmin):
fields = ['extra_data', 'title'] fields = ['extra_data', 'title']


validate(FieldsOnFormOnlyAdmin, Song) validate(FieldsOnFormOnlyAdmin, Song)

def test_modelform_with_other_model(self):
"""
The model specified in ModelForm.Meta should not limit the available
fields.

Bug #17428: Admin formfield validation uses form model instead of
registered model.
"""
class AuthorForm(forms.ModelForm):
class Meta:
model = Author

class SongAdmin(admin.ModelAdmin):
form = AuthorForm
fields = ['title']

validate(SongAdmin, Song)
self.assertRaisesMessage(ImproperlyConfigured,
"'SongAdmin.fields' refers to field 'title' that is missing from the form.",
validate,
SongAdmin, Author)