Skip to content

fix: handle non-string initial values in disabled TagField - #949

Open
SparshGarg999 wants to merge 1 commit into
jazzband:masterfrom
SparshGarg999:fix-disabled-tagfield-clean
Open

fix: handle non-string initial values in disabled TagField#949
SparshGarg999 wants to merge 1 commit into
jazzband:masterfrom
SparshGarg999:fix-disabled-tagfield-clean

Conversation

@SparshGarg999

Copy link
Copy Markdown

Description

Fixes #943.

When a form field using TagField is set to disabled=True, Django's form handling passes the field's initial value directly into clean(self, value).

For ModelForms (or forms where initial data is provided as a QuerySet or list of Tag model instances), super().clean(value) converts the list of tag instances into its string representation ('[<Tag: apple>, <Tag: banana>]'). This was subsequently parsed by parse_tags(), corrupting the tag names into strings like '[<Tag: apple>' and '<Tag: banana>]'.

Changes

  • In taggit/forms.py, updated TagField.clean(self, value):
    • When self.disabled is True, if value is not a string, return [] if value is None else value.
    • If value is a string, clean and parse it normally.
  • In tests/test_forms.py, added unit tests for:
    • Disabled TagField with a list of Tag model instances.
    • Disabled TagField with a string initial value.
    • Disabled TagField with None initial value.
    • Disabled TagField on a ModelForm (FoodForm), ensuring form.save() preserves the original model tags untouched.
  • In CHANGELOG.rst, added an entry under (Unreleased).

Validation

  • Ran python -m django test --settings=tests.settings (407 tests passed).
  • Verified lint and code formatting with black, flake8, and isort.

When TagField has disabled=True, Django passes the initial value into clean().
For ModelForms or initial values providing model instances or tag lists,
super().clean() converted the list to a string ('[<Tag: ...>]') which was
subsequently corrupted by parse_tags().

Safely return non-string values and handle string/None initials when disabled.

Fixes jazzband#943
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Disabled TagField messes data up

1 participant