Django表单 - 附加到类的meta排除和小部件 [英] Django forms - append to class meta exclude and widgets

查看:143
本文介绍了Django表单 - 附加到类的meta排除和小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以附加到继承的表单的排除或小部件变量?



我到目前为止设置了以下设置。

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

我试图让EditPropertyForm的最终结果看起来像排除和小部件语句一样。

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

如果有一个更好的方法,请建议。



非常感谢任何帮助。

解决方案

从t获取属性怎么样?他的父母Meta类和更新他们?



这样的东西(未经测试):

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

它不漂亮,但应该让你想要的。


Is it possible to append to the exclude or widgets variables of an inherited Form?

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.

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?

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.

这篇关于Django表单 - 附加到类的meta排除和小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆