Skip to content

Commit f0be8ac

Browse files
more tagulous select field fixes
1 parent 7b4187a commit f0be8ac

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

dojo/filters.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -343,32 +343,35 @@ 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-
# 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
346+
# Don't delete filters - django-filters needs them for queryset processing
347+
# We only replace the form field to use tagulous instead of ModelMultipleChoiceFilter
354348

355-
# Get the tagulous field from the model
356-
tagulous_field = model._meta.get_field("tags").formfield()
349+
# Get the correct model for this specific field
350+
tagged_model, exclude = get_tags_model_from_field_name(field)
351+
if tagged_model:
352+
# Use the specific model for this field (e.g., Test for test__tags, Engagement for test__engagement__tags)
353+
tagulous_field = tagged_model._meta.get_field("tags").formfield()
354+
# Fall back to the main model's tags field for the basic "tags" field
355+
elif "tags" in self.filters:
356+
main_model = self.filters["tags"].model
357+
tagulous_field = main_model._meta.get_field("tags").formfield()
358+
else:
359+
continue # Skip this field if no model available
357360

358-
# we defer applying the select2 autocomplete because there can be multiple forms on the same page
359-
# and form.js would then apply select2 multiple times, resulting in duplicated fields
360-
# the initialization now happens in filter_js_snippet.html
361-
tagulous_field.widget.tag_options += tagulous.models.options.TagOptions(autocomplete_settings={"width": "200px", "defer": True})
361+
# Remove defer to prevent timing issues that cause field duplication
362+
# Let tagulous initialize normally instead of deferring to filter_js_snippet.html
363+
tagulous_field.widget.tag_options += tagulous.models.options.TagOptions(autocomplete_settings={"width": "200px", "defer": False})
362364

363-
tagged_model, exclude = get_tags_model_from_field_name(field)
365+
# Set the label and autocomplete tags for the field
364366
if tagged_model: # only if not the normal tags field
365367
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")
368+
# Set autocomplete_tags to the correct model's tags
369+
tagulous_field.autocomplete_tags = tagged_model.tags.tag_model.objects.all().distinct().order_by("name")
367370

368371
if exclude:
369372
tagulous_field.label = "Not " + tagulous_field.label
370373

371-
# Replace the field completely
374+
# Replace the field completely in the form
372375
self.form.fields[field] = tagulous_field
373376

374377

dojo/templates/base.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,7 @@ <h4 class="modal-title" id="sessionModalLabel">Session Expiring Soon</h4>
12191219
<!-- 'no_results_text': "Tag not found, press TAB key to add.", -->
12201220
<!-- }); -->
12211221

1222-
$('select').not('#notification-scope').addClass('selectpicker');
1222+
$('select').not('#notification-scope').not('[data-tagulous]').not('.tagulous-select').addClass('selectpicker');
12231223
$('.selectpicker').attr('data-live-search', 'true');
12241224
$('.selectpicker').attr('data-container', 'body');
12251225
$('.selectpicker').css('width', '70%');

dojo/templates/dojo/filter_js_snippet.html

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
$(this).select2(
1515
)
1616
})
17-
$('input[data-tagulous]').each(function () {
18-
Tagulous.select2($(this))
19-
})
17+
// Tagulous initialization now handled automatically by the field (defer=False)
18+
// No manual initialization needed to prevent double initialization
2019
})
2120

2221

0 commit comments

Comments
 (0)