Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Changelog
* Add Django 5.2 and 6.0 support
* Add Python 3.13 and 3.14 support
* Fix an issue where the admin merge tag form redirect would fail when querystrings are present inside the URL
* Mark ``add``/``set``/``remove``/``clear`` on ``_TaggableManager`` with ``alters_data = True`` so templates cannot invoke them

6.1.0 (2024-09-29)
~~~~~~~~~~~~~~~~~~
Expand Down
8 changes: 8 additions & 0 deletions taggit/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ def add(self, *tags, through_defaults=None, tag_kwargs=None, **kwargs):
using=db,
)

add.alters_data = True

def _to_tag_model_instances(self, tags, tag_kwargs):
"""
Takes an iterable containing either strings, tag objects, or a mixture
Expand Down Expand Up @@ -340,6 +342,8 @@ def set(self, tags, *, through_defaults=None, **kwargs):
self.remove(*old_tag_strs)
self.add(*new_objs, through_defaults=through_defaults, **kwargs)

set.alters_data = True

@require_instance_manager
def remove(self, *tags):
if not tags:
Expand Down Expand Up @@ -376,6 +380,8 @@ def remove(self, *tags):
using=db,
)

remove.alters_data = True

@require_instance_manager
def clear(self):
self._remove_prefetched_objects()
Expand Down Expand Up @@ -403,6 +409,8 @@ def clear(self):
using=db,
)

clear.alters_data = True

def most_common(self, min_count=None, extra_filters=None):
queryset = (
self.get_queryset(extra_filters)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ def test_duplicates(self):
desired_result = ["green"]
self.assertEqual(desired_result, [tag.name for tag in sample_obj.tags.all()])

def test_mutating_methods_mark_alters_data(self):
sample_obj = TestModel.objects.create()
for name in ("add", "set", "remove", "clear"):
method = getattr(sample_obj.tags, name)
self.assertTrue(
getattr(method, "alters_data", False),
f"{name} should be marked alters_data",
)


class TestSlugification(TestCase):
def test_unicode_slugs(self):
Expand Down
Loading