我正在尝试创建一个自定义Django ModelForm元类,该类将允许对从源模型继承的Form字段进行一些额外的自定义,例如:
之所以这样做,是因为我需要几个不同的ModelForms用于单个Model,每个都有上述选项的变体。这是我的元类:
class CustomModelFormMetaclass(models.ModelFormMetaclass):
"""
Custom ModelForm metaclass which adds extra configuration of fields inherited from Model
"""
def __new__(mcs, name, bases, attrs):
new_class = super(models.ModelFormMetaclass, mcs).__new__(mcs, name, bases, attrs)
# Set initial field values
#initials = attrs.get('initial_values')
for field_name, initial_value in new_class.initial_values.items():
field = new_class.base_fields.get(field_name, None)
if field and field.initial is None:
field.initial = initial_value
# Set refined choices
#refined_choices = attrs.get('refined_choices')
for field_name, refined_choices in new_class.refined_choices.items():
set_field_choices(new_class, field_name,refined_choices)
# Set disabled fields
for field_name in new_class.read_only_fields:
field = new_class.base_fields.get(field_name, None)
if field:
#field.disabled=True
field.widget.attrs['readonly'] = True
# Set required fields
for field_name in new_class.required_fields:
field = new_class.base_fields.get(field_name, None)
if field:
field.required=True
# Set DateTime and Date help texts
for field in new_class.base_fields.values():
if field.help_text is None:
if type(field).__name__ == 'DateTimeField':
field.help_text = 'YYYY-MM-DD HH:MM:SS'
elif type(field).__name__ == 'DateField':
field.help_text = 'YYYY-MM-DD'
return new_class
以及将作为实际ModelForms父类的基本自定义ModelForm类:
class CustomModelForm(forms.BaseModelForm, metaclass=CustomModelFormMetaclass):
method='post'
target=None
target_params={}
refined_choices={}
initial_values={}
read_only_fields=[]
required_fields= []
一个这样的例子是(这种情况没有利用我的自定义参数):
class LBSListAdminForm(darwin_forms.CustomModelForm):
method = 'get'
class Meta:
model = LBSListRequest
exclude = ['start_time', 'site', 'process_id', 'user', 'duration', 'method', 'request_data', 'status', 'response']
labels = {}
help_texts = {}
error_messages = {}
widgets = {}
但是我遇到此错误:
“ LBSListAdminForm”对象没有属性“ _meta”
我在这里做错了什么?具有多个继承级别的metclass链变得令人困惑。感谢任何帮助
欢呼
答案 0 :(得分:0)
_meta
属性由ModelFormMetaclass
中的__new__
设置。问题是您没有在子类中正确调用super()
,因此__new__
中没有调用ModelFormMetaclass
,因此未设置_meta
。 / p>
对super()
的调用应将当前类作为其第一个参数,因此应读为:
new_class = super(models.CustomModelFormMetaclass, mcs).__new__(mcs, name, bases, attrs)