#27947 closed Cleanup/optimization (fixed)
Document that model field error_messages don't carry over to forms
| Reported by: | Jimmy Merrild Krag | Owned by: | Taavi Teska |
|---|---|---|---|
| Component: | Documentation | Version: | 1.8 |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Ready for checkin | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
I have the following field in a model:
reference = models.SlugField(
max_length=20,
blank=False, null=False,
error_messages={
'invalid': _('A reference may only contain letters, numbers, underscores and hyphens.'),
})
however, when submitting the field in a ModelForm with invalid content (e.g. containing a space character) the error message is the default one for SlugField and not the one I set.
Change History (7)
comment:1 by , 9 years ago
| Component: | Uncategorized → Documentation |
|---|---|
| Summary: | error_messages does not work on SlugField in ModelForm → Document that model field error_messages don't carry over to forms |
| Triage Stage: | Unreviewed → Accepted |
| Type: | Bug → Cleanup/optimization |
comment:2 by , 9 years ago
So how should I override my error messages?
Right now I do
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
# Correct error message for reference field
reference_field = Plan._meta.get_field('reference')
self.fields['reference'].error_messages.update(reference_field.error_messages)
which I think is ugly
comment:3 by , 9 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
Note:
See TracTickets
for help on using tickets.
I was surprised about this behavior but according to ticket:13693#comment:10, it's expected. The documentation should clarify this.