Django forms - adding meta exclude class and widgets

Can legacy variables be added to exceptions or widgets?

I have the following setup.

class AddPropertyForm(forms.ModelForm): num_months = forms.ChoiceField(choices=MONTHS) request_featured = forms.BooleanField(required=False) featured_months = forms.ChoiceField(choices=MONTHS) class Meta(): model = RentalProperty exclude = ('listing_id', 'active_listing', 'active_listing_expiry_date', 'featured_property', 'featured_expiry_date', 'slug', 'property_manager') widgets = { 'property_type': forms.Select(attrs={'onchange':'propertyType()'}), } class EditPropertyForm(AddPropertyForm): request_reactivation = forms.BooleanField(required=False) class Meta(AddPropertyForm.Meta): exclude = ('address1', 'property_type') widgets = { 'request_reactivation': forms.CheckboxInput(attrs {'onchange':'reactivateProperty()'}), } 

I am trying to get the final result for EditPropertyForm to look like this for exclude and widgets statements.

 exclude = ('address1', 'property_type', 'listing_id', 'active_listing', 'active_listing_expiry_date', 'featured_property', 'featured_expiry_date', 'slug', 'property_manager') widgets = { 'request_reactivation': forms.CheckboxInput(attrs {'onchange':'reactivateProperty()'}), 'property_type': forms.Select(attrs={'onchange':'propertyType()'}), } 

If there is a better approach, suggest.

Any help is most appreciated.

+6
source share
1 answer

How to capture properties from the parent Meta class and update them?

Something like this (untested):

 class Meta(AddPropertyForm.Meta): exclude = tuple(list(AddPropertyForm.Meta.exclude) + ['address1', 'property_type']) widgets = AddPropertyForm.Meta.widgets.copy() widgets.update({ 'request_reactivation': forms.CheckboxInput(attrs {'onchange':'reactivateProperty()'}), }) 

This is ugly, but you should get what you want.

+6
source

Source: https://habr.com/ru/post/896273/


All Articles