Code

Opened 7 years ago

Closed 7 years ago

#4727 closed (fixed)

[newforms-admin] allow override of forms.BaseForm as a base class for admin forms

Reported by: Honza Král <Honza.Kral@…> Owned by: Honza_Kral
Component: contrib.admin Version: newforms-admin
Severity: Keywords: newforms admin form class
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

I have a model that need some custom validation across multiple fields. To achieve that it would be best to allow overriding of base form like this:

from django import newforms as forms
class SomeModelForm( forms.BaseForm ):
    def clean( self ):
        data = self.cleaned_data
        if I_not_like_something( data ):
            raise forms.ValidationError, _( "Something is wrong" )
        return data

class SomeModelOptions( admin.ModelAdmin ):
    base_form = SomeModelForm

admin.site.register( SomeModel, SomeModelOptions )

Since newforms.models.form_for_instance and newforms.models.form_for_instance already provide this functionality, its just a matter of adding the variable to ModelAdmin.

This approach would also solve #4507

Attachments (2)

newforms-admin-5559-allow-form-base-override.diff (1.8 KB) - added by Honza Král <Honza.Kral@…> 7 years ago.
newforms-admin-base-form.diff (4.2 KB) - added by Petr Marhoun <petr.marhoun@…> 7 years ago.

Download all attachments as: .zip

Change History (8)

Changed 7 years ago by Honza Král <Honza.Kral@…>

comment:1 Changed 7 years ago by SmileyChris

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed

comment:2 Changed 7 years ago by anonymous

+1 on this -- very easy patch provides significant new functionality.

This seems to me the easiest way of handling what I assume is a very common use case of validating across multiple fields in the admin.

comment:3 Changed 7 years ago by Honza_Kral

  • Owner changed from nobody to Honza_Kral

Changed 7 years ago by Petr Marhoun <petr.marhoun@…>

comment:4 Changed 7 years ago by Petr Marhoun <petr.marhoun@…>

I attached a new patch which works also for inline formsets.

comment:5 Changed 7 years ago by Petr Marhoun <petr.marhoun@…>

I don't think that attached patches solve #4507 - see my comments.

comment:6 Changed 7 years ago by Honza_Kral

  • Resolution set to fixed
  • Status changed from new to closed

fixed by add_form and change_form hooks

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.