Skip to content

Commit

Permalink
Fix TagField.has_changed for empty values (#757)
Browse files Browse the repository at this point in the history
* Add a reference to the form saving docs.

 This should let us piggyback off of Django's nice explanations.

* Fix TagField.has_changed for empty values

Thanks to [this report](#749) for the
initial report and pointing to a potential fix.
  • Loading branch information
rtpg committed Oct 11, 2021
1 parent dcd39a1 commit cbfbe22
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog
(Unreleased)
~~~~~~~~~~~~

* Fix issue where ``TagField`` would incorrectly report that a field has changed on empty values.
* Update Russian translation.
* Provide translators additional context regarding strings in TagBase model.

Expand Down
2 changes: 2 additions & 0 deletions docs/forms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,5 @@ form with normal many to many fields on it::
obj.save()
# Without this next line the tags won't be saved.
form.save_m2m()

You can check the details over in the `Django documentation on form saving <https://docs.djangoproject.com/en/3.2/topics/forms/modelforms/#the-save-method>`_.
5 changes: 4 additions & 1 deletion taggit/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ def has_changed(self, initial_value, data_value):
except forms.ValidationError:
pass

if initial_value is None:
# normalize "empty values"
if not data_value:
data_value = []
if not initial_value:
initial_value = []

initial_value = [tag.name for tag in initial_value]
Expand Down
8 changes: 8 additions & 0 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,11 @@ class TestForm(forms.Form):
)

self.assertFalse(form.has_changed())

def test_should_return_False_if_not_provided(self):
class TestForm(forms.Form):
tag = TagField()

form = TestForm()

self.assertFalse(form.has_changed())

0 comments on commit cbfbe22

Please sign in to comment.