Skip to content

Select2Widget ignores empty_label  #565

@dyve

Description

@dyve

Describe the bug
When using Select2Widget on a field that has empty_label set, the empty_label setting is ignored. I would expect the content of empty_label (if any) as the default value for data-placeholder in the attrs of select2Widget.

Code Snippet

class BookForm(forms.ModelForm):
    """Model Book has field user that is ForeignKey to User."""
    class Meta:
        model = Book
        fields = ["user"]
        widgets = {"user": Select2Widget}

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields["user"].empty_label = "No user is fine too."
        # self.fields["user"].widget.attrs['data-placeholder'] = self.fields["user"].empty_label
        # Uncomment the line above to make the empty_label appear as the placeholder

To Reproduce
Steps to reproduce the behavior:

  1. Use the form from the example.
  2. See that "No user is fine too." does NOT appear.
  3. Uncomment the line that enables the workaround.
  4. See that "No user is fine too." DOES appear.

Expected behavior
When a field has an empty_label, I expect that to be the default value for Select2Widget.attrs["data-placeholder"]

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions