Code


Version 3 (modified by anonymous, 6 years ago) (diff)

--

Part of DjangoSpecifications

Media and validation in newforms-admin

Attaching media (especially JavaScript) and custom validation is a common use case for admin section. It should be easy.

Currently, get_form() can be used to provide a custom form that can bring in JavaScript and custom validation. But once this method is overriden, your form will not automatically get the custom admin widgets. As the automatic widgets are the main point of using admin, this is not generally acceptable.

Proposal

Customizing the admin should be possible by specifying an extension form for the admin class, preferrably with a class field: class FooAdmin(admin.ModelAdmin): admin_form = FooAdminForm. Overriding get_form() is equally acceptable.

  • Specifying this custom form should not disable automatic widgets
  • JavaScript, CSS and whatnot should be merged from the inner Media class if it is present in the custom form: class FooAdminForm(forms.BaseForm): media = forms.Media(js = ['/site_media/js/tiny_mce/tiny_mce.js'])
  • Custom validation should be possible with the ordinary clean_field() and clean() overrides in the custom form and should not interfere with base field validation.