Opened 17 years ago

Closed 17 years ago

Last modified 17 years ago

#6866 closed (fixed)

Easy custom media and validation in newforms-admin

Reported by: mrts Owned by: nobody
Component: contrib.admin Version: newforms-admin
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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

As of [7278], get_form() can be used to provide a custom form that can bring in JavaScript and custom validation. Due to bugs, 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.

The proposed behaviour (that is implemented in the attached patch -- thanks, Brosner, for the bug fix!) is as follows:

# A Foo with unique language
class Foo(models.Model):
    foo = models.TextField()
    language = models.CharField(max_length=3, choices=LANG_CHOICES, unique=True)
    def __unicode__(self):
        return 'Foo in %s' % self.get_language_display()

# common base class for attaching TinyMCE to several validator forms
class TinyMCEForm(forms.ModelForm):
    media = forms.Media(
        js = ['/site_media/js/tiny_mce/tiny_mce.js',
                '/site_media/js/add_editor.js'])

# a common validator form for all models with a 'language' field
class LanguageValidatorForm(TinyMCEForm):
    def clean_language(self):
        if 'language' not in self.cleaned_data:
            return
        lang = self.cleaned_data['language']
        model = self._meta.model
        if model.objects.filter(language = lang).exclude(pk=self.instance.pk):
            raise forms.ValidationError('%(obj_title)s already exists for '
                    'language %(lang)s. Only one %(obj)s is allowed for '
                    'each language.'
                    % { 'obj_title' : model._meta.verbose_name.title(),
                        'obj' : model._meta.verbose_name,
                        'lang' : admin_forms.get_lang_display(lang)})

class LanguageAdmin(admin.ModelAdmin):
    admin_form = LanguageValidatorForm
    save_on_top = True

admin.site.register(Foo, LanguageAdmin)

I.e. there is a class variable that specifies the form that brings in both validation and media.

Attachments (3)

easy_validation_and_media.diff (2.8 KB ) - added by mrts 17 years ago.
Implementation of the proposal.
easy_validation_and_media-with-fixed-inlines.diff (3.7 KB ) - added by mrts 17 years ago.
Fixes inline forms as well.
validation_and_media-with-basemodelform.diff (2.7 KB ) - added by mrts 17 years ago.
Use BaseModelForm throughout, accept it as a parent in ModelFormMetaclass

Download all attachments as: .zip

Change History (6)

by mrts, 17 years ago

Implementation of the proposal.

by mrts, 17 years ago

Fixes inline forms as well.

comment:1 by mrts, 17 years ago

I was over-enthusiastic, the patch does not fix the widget breakage problem.

by mrts, 17 years ago

Use BaseModelForm throughout, accept it as a parent in ModelFormMetaclass

comment:2 by Brian Rosner, 17 years ago

Resolution: fixed
Status: newclosed

(In [7360]) newforms-admin: Fixed #6866 -- ModelAdmin.get_form and InlineModelAdmin.get_formset now uses BaseModelAdmin.form so the form class can be changed out very easily.

comment:3 by Brian Rosner, 17 years ago

It is a tad too late, but I did forget to thank mrts for his work on this. So, thanks, mrts. :)

Note: See TracTickets for help on using tickets.
Back to Top