Opened 4 years ago

Closed 4 years ago

#12341 closed (duplicate)

unique_error_message() assumes field has label attribute, generates bad error message if not

Reported by: margieroginski Owned by: nobody
Component: Uncategorized Version: 1.1
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


I have a model form and have overridden one of the fields in the form. IE, I created a field that looks like this:

class CharField_CV(CharField):
    def __init__(self, *args, **kwargs):
        kwargs['show_hidden_initial'] = True
        super(CharField_CV, self).__init__(*args, **kwargs)

In my form I have code like this:

class ProjectForm(forms.ModelForm):
    class Meta:
        model = Project
        fields = ("name", "owner", "status", "description")

    name = CharField_CV(max_length=Project._meta.get_field('name').max_length)

I find that because I did not use the label argument in my call to CharField_CV(), the label attribute of the field is not set. In my Project model, name is declared like this:

name=models.CharField(max_length=30, unique=True)

It seems that when BaseModelForm::unique_error_message() is called, it assumes that the field has a label attribute and the result is that the error message (in the case where the user specified a name that was not unique) is:

Project with this None already exists

Other parts of the code take care of this by setting label based on the name of the field, if the label attribute doesn't exist. For example, when a BoundField() is created from the field, it does this in init:

        if self.field.label is None:
            self.label = pretty_name(name)
            self.label = self.field.label

I realize this is a corner case bug and completely workaroundable (just specify the label argument when instantiating the field), but since I identified it, I just wanted to post it so that it is logged in case anyone else encounters it.

Attachments (0)

Change History (1)

comment:1 Changed 4 years ago by kmtracey

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed

This and #12304 are really the same problem, one is the unique error message and the other is the unique_together one. They should be fixed as one problem.

Add Comment

Modify Ticket

Change Properties
<Author field>
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.