Opened 4 years ago

Closed 3 years ago

Last modified 3 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 Changed 4 years ago by Tim Graham

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 Changed 4 years ago by Jimmy Merrild Krag

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 Changed 3 years ago by Taavi Teska

Owner: changed from nobody to Taavi Teska
Status: newassigned

comment:4 Changed 3 years ago by Taavi Teska

Has patch: set

comment:5 Changed 3 years ago by Rivo Laks

Triage Stage: AcceptedReady for checkin

LGTM

comment:6 Changed 3 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In 5dbf1c4b:

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

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

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