Skip to content

Commit

Permalink
Make TaggableManager.set API compatible with Django's RelatedManager.…
Browse files Browse the repository at this point in the history
…set (#763)

* Make TaggableManager.set API compatible with Django's RelatedManager.set

* Fix TaggableManager.set documentation

* Add example of TaggableManager.set new API

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
pablolmedorado and pre-commit-ci[bot] committed Nov 11, 2021
1 parent 559bf84 commit 8ada5b7
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ Florian Apolloner <apollo13@apolloner.eu>
Andrew Pryde <andrew@rocketpod.co.uk>
John Whitlock <jwhitlock@mozilla.com>
Jon Dufresne <jon.dufresne@gmail.com>
Pablo Olmedo Dorado <pablolmedorado@gmail.com>
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ Changelog
(Unreleased)
~~~~~~~~~~~~

* **Backwards incompatible:** ``TaggableManager.set`` now takes a list of tags
(instead of varargs) so that its API matches Django's ``RelatedManager.set``. Example:

- previously: ``item.tags.set("red", "blue")``
- now: ``item.tags.set(["red", "blue"])``

* 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: 1 addition & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ playing around with the API.

Removes all tags from an object.

.. method:: set(*tags, through_defaults=None, clear=False)
.. method:: set(tags, *, through_defaults=None, clear=False)

If ``clear = True`` removes all the current tags and then adds the
specified tags to the object. Otherwise sets the object's tags to those
Expand Down
4 changes: 2 additions & 2 deletions taggit/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def slugs(self):
return self.get_queryset().values_list("slug", flat=True)

@require_instance_manager
def set(self, *tags, through_defaults=None, **kwargs):
def set(self, tags, *, through_defaults=None, **kwargs):
"""
Set the object's tags to the given n tags. If the clear kwarg is True
then all existing tags are removed (using `.clear()`) and the new tags
Expand Down Expand Up @@ -553,7 +553,7 @@ def post_through_setup(self, cls):
)

def save_form_data(self, instance, value):
getattr(instance, self.name).set(*value)
getattr(instance, self.name).set(value)

def formfield(self, form_class=TagField, **kwargs):
defaults = {
Expand Down
2 changes: 1 addition & 1 deletion taggit/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def update(self, instance, validated_data):
def _save_tags(self, tag_object, tags):
for key in tags.keys():
tag_values = tags.get(key)
getattr(tag_object, key).set(*tag_values)
getattr(tag_object, key).set(tag_values)

return tag_object

Expand Down
4 changes: 2 additions & 2 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def test_set_with_clear_true_sends_m2m_changed_signal(self, send_mock):
apple.tags.add("red")
send_mock.reset_mock()

apple.tags.set("red", clear=True)
apple.tags.set(["red"], clear=True)

red_pk = self.tag_model.objects.get(name="red").pk

Expand Down Expand Up @@ -429,7 +429,7 @@ def test_set_sends_m2m_changed_signal(self, send_mock):
apple.tags.add("green")
send_mock.reset_mock()

apple.tags.set("red")
apple.tags.set(["red"])

green_pk = self.tag_model.objects.get(name="green").pk
red_pk = self.tag_model.objects.get(name="red").pk
Expand Down

0 comments on commit 8ada5b7

Please sign in to comment.