Django

Code

Ticket #6866 (closed: fixed)

Opened 2 months ago

Last modified 2 months ago

Easy custom media and validation in newforms-admin

Reported by: mrts Assigned to: nobody
Component: Admin interface Version: newforms-admin
Keywords: Cc:
Triage Stage: Unreviewed Has patch: 1
Needs documentation: 0 Needs tests: 0
Patch needs improvement: 0

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

easy_validation_and_media.diff (2.8 kB) - added by mrts on 03/23/08 21:07:44.
Implementation of the proposal.
easy_validation_and_media-with-fixed-inlines.diff (3.7 kB) - added by mrts on 03/23/08 22:32:54.
Fixes inline forms as well.
validation_and_media-with-basemodelform.diff (2.7 kB) - added by mrts on 03/24/08 14:31:10.
Use BaseModelForm? throughout, accept it as a parent in ModelFormMetaclass?

Change History

03/23/08 21:07:44 changed by mrts

  • attachment easy_validation_and_media.diff added.

Implementation of the proposal.

03/23/08 22:32:54 changed by mrts

  • attachment easy_validation_and_media-with-fixed-inlines.diff added.

Fixes inline forms as well.

03/24/08 11:45:56 changed by mrts

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

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

03/24/08 14:31:10 changed by mrts

  • attachment validation_and_media-with-basemodelform.diff added.

Use BaseModelForm? throughout, accept it as a parent in ModelFormMetaclass?

03/24/08 18:46:26 changed by brosner

  • status changed from new to closed.
  • resolution set to fixed.

(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.

03/24/08 18:47:40 changed by brosner

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


Add/Change #6866 (Easy custom media and validation in newforms-admin)




Change Properties
Action