Skip to content

Commit 7b4187a

Browse files
tagulous: remove duplicated fields
1 parent 04f9015 commit 7b4187a

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

dojo/filters.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -343,21 +343,33 @@ def __init__(self, *args, **kwargs):
343343
for field in ["tags", "test__tags", "test__engagement__tags", "test__engagement__product__tags",
344344
"not_tags", "not_test__tags", "not_test__engagement__tags", "not_test__engagement__product__tags"]:
345345
if field in self.form.fields:
346-
tags_filter = self.filters["tags"]
347-
model = tags_filter.model
346+
# Remove the original filter field to prevent duplication
347+
if field in self.filters:
348+
tags_filter = self.filters[field]
349+
model = tags_filter.model
350+
else:
351+
# Fallback to the 'tags' filter if specific field filter doesn't exist
352+
tags_filter = self.filters["tags"]
353+
model = tags_filter.model
354+
355+
# Get the tagulous field from the model
356+
tagulous_field = model._meta.get_field("tags").formfield()
348357

349-
self.form.fields[field] = model._meta.get_field("tags").formfield()
350358
# we defer applying the select2 autocomplete because there can be multiple forms on the same page
351359
# and form.js would then apply select2 multiple times, resulting in duplicated fields
352360
# the initialization now happens in filter_js_snippet.html
353-
self.form.fields[field].widget.tag_options += tagulous.models.options.TagOptions(autocomplete_settings={"width": "200px", "defer": True})
361+
tagulous_field.widget.tag_options += tagulous.models.options.TagOptions(autocomplete_settings={"width": "200px", "defer": True})
362+
354363
tagged_model, exclude = get_tags_model_from_field_name(field)
355364
if tagged_model: # only if not the normal tags field
356-
self.form.fields[field].label = get_tags_label_from_model(tagged_model)
357-
self.form.fields[field].autocomplete_tags = tagged_model.tags.tag_model.objects.all().order_by("name")
365+
tagulous_field.label = get_tags_label_from_model(tagged_model)
366+
tagulous_field.autocomplete_tags = tagged_model.tags.tag_model.objects.all().order_by("name")
358367

359368
if exclude:
360-
self.form.fields[field].label = "Not " + self.form.fields[field].label
369+
tagulous_field.label = "Not " + tagulous_field.label
370+
371+
# Replace the field completely
372+
self.form.fields[field] = tagulous_field
361373

362374

363375
def get_tags_model_from_field_name(field):

0 commit comments

Comments
 (0)