简体   繁体   English

Django表单 - 附加到类元排除和小部件

[英]Django forms - append to class meta exclude and widgets

Is it possible to append to the exclude or widgets variables of an inherited Form? 是否可以附加到继承表单的exclude或widgets变量?

I have the following set up so far. 到目前为止我有以下设置。

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 end result for EditPropertyForm to look like the following for the exclude and widgets statements. 我试图让EditPropertyForm的最终结果看起来像下面的exclude和widgets语句。

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, please suggest. 如果有更好的方法,请建议。

Any help is most appreciated. 任何帮助都非常感谢。

What about grabbing the properties from the parent Meta class and updating them? 从父类Meta中获取属性并更新它们怎么样?

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()'}),
    })

It's not pretty, but should get you what you want. 它不漂亮,但应该得到你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM