Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Closed
wants to merge 3 commits into from

2 participants

@per42

The model specified in ModelForm.Meta should not limit the
available fields. This solution uses the same modelform factory used by
the ModelAdmin.get_form method, which adds the registered model's
fields.

https://code.djangoproject.com/ticket/17428

This is my first django core contribution, submitted during the Stockholm sprint. Please double check that I don't break anything. I expect it to be rejected ;)

per42 added some commits
@per42 per42 Bug #17428: Admin formfield validation uses form model instead
of registered model.

The model specified in ModelForm.Meta should not limit the
available fields. This solution uses the same modelform factory used by
the ModelAdmin.get_form method, which adds the registered model's
fields.
05150b3
@per42 per42 Merge remote-tracking branch 'django/stable/1.5.x' into fix/17428 f4f43c5
@per42 per42 Merge remote-tracking branch 'django/stable/1.5.x' into fix/17428 fca8a07
@timgraham
Owner

Ticket has been closed as the validation in question has been removed.

@timgraham timgraham closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 17, 2012
  1. @per42

    Bug #17428: Admin formfield validation uses form model instead

    per42 authored
    of registered model.
    
    The model specified in ModelForm.Meta should not limit the
    available fields. This solution uses the same modelform factory used by
    the ModelAdmin.get_form method, which adds the registered model's
    fields.
  2. @per42
  3. @per42
This page is out of date. Refresh to see the latest.
View
21 django/contrib/admin/validation.py
@@ -2,7 +2,7 @@
from django.db import models
from django.db.models.fields import FieldDoesNotExist
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.util import get_fields_from_path, NotRelationField
from django.contrib.admin.options import (flatten_fieldsets, BaseModelAdmin,
@@ -381,19 +381,12 @@ def get_field(cls, model, opts, label, field):
% (cls.__name__, label, field, model._meta.app_label, model.__name__))
def check_formfield(cls, model, opts, label, field):
- if getattr(cls.form, 'base_fields', None):
- try:
- cls.form.base_fields[field]
- except KeyError:
- raise ImproperlyConfigured("'%s.%s' refers to field '%s' that "
- "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))
+ form = modelform_factory(model, form=cls.form)
+ try:
+ form.base_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):
try:
View
24 tests/regressiontests/admin_validation/tests.py
@@ -6,7 +6,7 @@
from django.core.exceptions import ImproperlyConfigured
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):
@@ -281,3 +281,25 @@ class FieldsOnFormOnlyAdmin(admin.ModelAdmin):
fields = ['extra_data', 'title']
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)
Something went wrong with that request. Please try again.