Opened 15 years ago

Closed 15 years ago

#12341 closed (duplicate)

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

Reported by: Margie Roginski 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: no UI/UX: no

Description

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)
        else:
            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.

Change History (1)

comment:1 by Karen Tracey, 15 years ago

Resolution: duplicate
Status: newclosed

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.

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