@@ -343,32 +343,35 @@ def __init__(self, *args, **kwargs):
343
343
for field in ["tags" , "test__tags" , "test__engagement__tags" , "test__engagement__product__tags" ,
344
344
"not_tags" , "not_test__tags" , "not_test__engagement__tags" , "not_test__engagement__product__tags" ]:
345
345
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
354
348
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
357
360
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 })
362
364
363
- tagged_model , exclude = get_tags_model_from_field_name ( field )
365
+ # Set the label and autocomplete tags for the field
364
366
if tagged_model : # only if not the normal tags field
365
367
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" )
367
370
368
371
if exclude :
369
372
tagulous_field .label = "Not " + tagulous_field .label
370
373
371
- # Replace the field completely
374
+ # Replace the field completely in the form
372
375
self .form .fields [field ] = tagulous_field
373
376
374
377
0 commit comments