Model forms error messages

350 views
Skip to first unread message

Karol Sikora

unread,
May 2, 2012, 10:13:41 AM5/2/12
to django-d...@googlegroups.com
Hi,

According to my ticket: https://code.djangoproject.com/ticket/18237 and previous one eg.: https://code.djangoproject.com/ticket/13693 I would to discuss wider this case.
IMHO redefining field in model form subclass to set custom error messages is a hardly DRY violation. Currently we have error_messages parameter in model fields definition, why not use them as default(it custom messages are given in fields definition) in model form? As I know these messages are use in admin, so there is the most intuitive way. There are model forms, so by default getting error messages form error_messages in model definition is IMHO the best way. Another option is something like in this blog: http://blog.brendel.com/2012/01/django-modelforms-setting-any-field.html
I don't believe that redefining fields in subclass of model form is the best way and that all developers has the same opinion.

Carl Meyer

unread,
May 2, 2012, 10:23:54 AM5/2/12
to django-d...@googlegroups.com
Hi Karol,

On 05/02/2012 08:13 AM, Karol Sikora wrote:
> According to my ticket: https://code.djangoproject.com/ticket/18237 and
> previous one eg.: https://code.djangoproject.com/ticket/13693 I would to
> discuss wider this case.
> IMHO redefining field in model form subclass to set custom error
> messages is a hardly DRY violation. Currently we have error_messages
> parameter in model fields definition, why not use them as default(it
> custom messages are given in fields definition) in model form? As I know
> these messages are use in admin, so there is the most intuitive way.
> There are *model* forms, so by default getting error messages form
> error_messages in model definition is IMHO the best way. Another option
> is something like in this blog:
> http://blog.brendel.com/2012/01/django-modelforms-setting-any-field.html
> I don't believe that redefining fields in subclass of model form is the
> best way and that all developers has the same opinion.

I agree with jezdez that a Meta option as outlined in that blog post is
not good, but personally I do agree that #13693 should be reopened and
accepted. Honza outlined some valid specific concerns on that ticket,
but IMO (and as he says himself) they are addressable concerns that
should be taken into account by someone working on a patch, they are not
a reason to close the ticket.

Carl

Karol Sikora

unread,
May 2, 2012, 5:50:41 PM5/2/12
to django-d...@googlegroups.com
Here is my use case.

Model field definition:
    blog_url = models.URLField(verbose_name=u'Blog', blank=True, error_messages={'invalid': u'Adres bloga nie jest prawidłowy'})
    
To define error messages in form I have to write:
 blog_url = forms.CharField(error_messages={'invalid': u'Adres bloga nie jest prawidłowy'}, label=u'Blog', required=False)

So there are 3 DRY violation in one field(label - verbose_name, required - blank, error_messages - error messages)!
I can't understand why all remaining model option are translated to corresponding fields, but error_messages not. 
In my opinion this is a serious bug, not only feature request.
Reply all
Reply to author
Forward
0 new messages