0
votes

I am trying to create a custom Django ModelForm metaclass which will allow some extra customisation of Form fields inherited from the source Model, such as:

  • Limit choices to a subset of all available for a particular Model field
  • Define read only and required form fields
  • Set initial values for fields

I'm doing this because I need a few different ModelForms for a single Model, each with variations of the above options. Here's my metaclass:

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

And the base custom ModelForm class that will be parent class for actual ModelForms:

class CustomModelForm(forms.BaseModelForm, metaclass=CustomModelFormMetaclass):
method='post'
target=None
target_params={}

refined_choices={}

initial_values={}

read_only_fields=[]

required_fields= []

One such example is (this case doesn't take advantage of my custom parameters):

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 = {}

However I'm getting this error:

'LBSListAdminForm' object has no attribute '_meta'

What have I done wrong here? The metclass chain with multiple levels of inheritance is getting confusing. Appreciate any help

Cheers

1

1 Answers

0
votes

The _meta attribute is set by the ModelFormMetaclass in __new__. The problem is that you've not invoked super() correctly in your subclass, and thus __new__ has not been called in the ModelFormMetaclass, and hence _meta was not set.

The call to super() should take the current class as its first argument, so should read:

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