Django自定义ModelForm元类,“对象没有属性''u meta'”

2024-04-20 05:10:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个自定义Django ModelForm元类,它将允许对从源模型继承的表单字段进行一些额外的自定义,例如:

  • 将选择限制为特定模型字段的所有可用子集
  • 定义只读和必需的表单字段
  • 设置字段的初始值

我这样做是因为我需要为一个模型使用几个不同的模型表单,每个模型都有上述选项的变体。这是我的元类:

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

以及将作为实际模型窗体的父类的基本自定义ModelForm类:

^{pr2}$

一个这样的例子是(这个例子没有利用我的自定义参数):

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' object has no attribute '_meta'

我做错什么了?具有多个继承级别的metclass链越来越混乱。谢谢你的帮助

干杯


Tags: namein模型nonefieldfieldsnewfor
1条回答
网友
1楼 · 发布于 2024-04-20 05:10:16

_meta属性由__new__中的ModelFormMetaclass设置。问题是您没有在子类中正确调用super(),因此__new__没有在{}中调用,因此没有设置{}。在

super()的调用应将当前类作为其第一个参数,因此应改为:

new_class = super(models.CustomModelFormMetaclass, mcs).__new__(mcs, name, bases, attrs)

相关问题 更多 >