Opened 7 years ago

Closed 3 years ago

#8913 closed Bug (fixed)

Make the "must be unique" error messages in ModelForms customizable

Reported by: Alexander_Nesterov Owned by: leahculver
Component: Forms Version: master
Severity: Normal Keywords: forms, unique, validation
Cc: orokusaki, tinodb, mwarkentin, schickm, uznick@… Triage Stage: Ready for checkin
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

feature request (?)
model:

class Context(models.Model): 
    name        = models.CharField(unique=True)

form:

class ContextForm(forms.ModelForm): 
    name = forms.CharField() 
    class Meta: 
        model = Context

example:

form = ContextForm(request.POST)  
if form.is_valid(): 
    form.save()

Is there any way to override an error message of validate_unique: u"%(model_name)s with this %(field_label)s already exists."?

class ContextForm(forms.ModelForm): 
    name = forms.CharField(error_messages={'unique': u'My custom message'}) 

Attachments (3)

unique-error-message.diff (3.7 KB) - added by leahculver 4 years ago.
unique-error-message-with-docs.diff (4.6 KB) - added by leahculver 4 years ago.
Added documentation.
unique-error-message-with-docs-author-update.diff (5.0 KB) - added by leahculver 4 years ago.
Updated my email address in AUTHORS

Download all attachments as: .zip

Change History (20)

comment:1 Changed 7 years ago by anonymous

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Summary changed from "unique" in error_messages of ModelForm to Make the "must be unique" error messages in ModelForms customizable

comment:2 Changed 6 years ago by jacob

  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 5 years ago by orokusaki

  • Cc orokusaki added

See this thread before applying a patch. I'll work on a patch after 1.2 release is complete.

orokusaki

comment:4 Changed 5 years ago by tinodb

  • Cc tinodb added

comment:5 Changed 5 years ago by mwarkentin

  • Cc mwarkentin added

comment:6 follow-up: Changed 4 years ago by wildfire

  • Easy pickings unset
  • Severity set to Normal
  • Type set to Uncategorized

Is this something appropriate for Django 1.4 now?

comment:7 in reply to: ↑ 6 Changed 4 years ago by julien

  • Type changed from Uncategorized to New feature

Replying to wildfire:

Is this something appropriate for Django 1.4 now?

Not until there is a patch with tests and doc ;)

comment:8 Changed 4 years ago by schickm

  • Cc schickm added

comment:9 Changed 4 years ago by anonymous

  • Owner changed from nobody to leahculver
  • UI/UX unset

Changed 4 years ago by leahculver

comment:10 Changed 4 years ago by leahculver

  • Has patch set
  • Needs documentation set
  • Patch needs improvement set

I've added a rough patch for this. To specify a custom error message for the unique contraint, you'll need to do something like this:

class Context(models.Model): 
    name = models.CharField(error_messages={'unique': u'My custom message'}) 

I don't know a lot about ModelForms vs. Forms but I can't seem to get this to work for plain Forms. Should this also be a Form error_message key?

I'm also not sure where to put the docs since the Model error_messages parameter is documented but lacking specificity (https://docs.djangoproject.com/en/1.3/ref/models/fields/#error-messages). Should I make this more specific?

comment:11 Changed 4 years ago by anonymous

You've got a bare except on #168, with a ambiguous failure message. I'd just remove that altogether.

Changed 4 years ago by leahculver

Added documentation.

comment:12 Changed 4 years ago by leahculver

  • Needs documentation unset

comment:13 Changed 4 years ago by andrewgodwin

  • Patch needs improvement unset
  • Triage Stage changed from Accepted to Ready for checkin

comment:14 Changed 4 years ago by leahculver

I've created a new ticket (#16192) for unique_together custom error messages since this is more difficult.

Changed 4 years ago by leahculver

Updated my email address in AUTHORS

comment:15 Changed 4 years ago by andrewgodwin

  • Resolution set to fixed
  • Status changed from new to closed

In [16345]:

Fixed #8913 - Make "must be unique" error messages customisable. Thanks to Leah Culver.

comment:16 Changed 3 years ago by Uznick <uznick@…>

  • Cc uznick@… added
  • Has patch unset
  • Patch needs improvement set
  • Resolution fixed deleted
  • Status changed from closed to reopened
  • Type changed from New feature to Bug
  • Version changed from 1.0 to SVN

The error message is still not customizable when it's set in the ModelForm.

Example:

class InformationSubscriber(models.Model):
    email = models.EmailField(u"E-mail", unique=True)

class InformationSubscribeForm(forms.ModelForm):
    email = forms.EmailField(label=u"E-mail", error_messages={'unique': u"You are already subscribed to our news",})

When the form is submitted, the error is: Information Subscriber with this E-mail already exists.

And when I set the error_messages in the Model, like

class InformationSubscriber(models.Model):
    email = models.EmailField(u"E-mail", unique=True, error_messages={'unique': u"You are already subscribed to our news",})

class InformationSubscribeForm(forms.ModelForm):
    email = forms.EmailField(label=u"E-mail")

The error message is correct: You are already subscribed to our news.

But the problem is, that there should be an option to set a unique error message on the form-level, not on the level of the whole model itself.

comment:17 Changed 3 years ago by claudep

  • Resolution set to fixed
  • Status changed from reopened to closed

I would suggest to open a new ticket, as allowing the message to be set on the form level is a different story. Currently, validate_unique may produce several errors to pass to ValidationError, so we cannot simply set code and params to be able to catch it at the form level.

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