Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#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 Tim Graham, 7 years ago

Component: UncategorizedDocumentation
Summary: error_messages does not work on SlugField in ModelFormDocument that model field error_messages don't carry over to forms
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

I was surprised about this behavior but according to ticket:13693#comment:10, it's expected. The documentation should clarify this.

comment:2 by Jimmy Merrild Krag, 7 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 Taavi Teska, 7 years ago

Owner: changed from nobody to Taavi Teska
Status: newassigned

comment:4 by Taavi Teska, 7 years ago

Has patch: set

comment:5 by Rivo Laks, 7 years ago

Triage Stage: AcceptedReady for checkin

LGTM

comment:6 by Tim Graham <timograham@…>, 7 years ago

Resolution: fixed
Status: assignedclosed

In 5dbf1c4b:

Fixed #27947 -- Doc'd that model Field.error_messages often don't propagate to forms.

comment:7 by Tim Graham <timograham@…>, 7 years ago

In c3d3aee:

[1.11.x] Fixed #27947 -- Doc'd that model Field.error_messages often don't propagate to forms.

Backport of 5dbf1c4b23cda915369f4895be293369575238d0 from master

Note: See TracTickets for help on using tickets.
Back to Top