Django docs state that a ModelForm will automatically set some field 
attributes for auto generated form fields. Those attributes are limited to : 
'required', 'verbose_name', 'help_text', and 'choices' (according to 
https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#django.forms.ModelForm
)

If the 'error_messages' attribute were also included, then there'd be just 
one place to define all your error messages: the model. This makes cleaner 
code, because you know there's only one way to look for error messages.

I use this function to make that happen:

def copy_model_error_messages( ModelFormType ):
        for ( field_name, formField, ) in ModelFormType.__dict__[ 
'base_fields' ].items():
                modelField = ModelFormType.Meta.model._meta.get_field( 
field_name )
                for ( message_key, message_value, ) in 
modelField.error_messages.items( ):
                        formField.error_messages[ message_key ] = 
message_value

I just call my function right after every ModelForm definition so that it 
gets it's error messages from the model fields:

class MyModelForm( forms.ModelForm ):
    ...
    class Meta:
        ...

copy_model_error_messages( MyModelForm )

Would you consider adding this feature to ModelForms?

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-developers?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to