Files
thrillwiki_django_no_react/memory-bank/features/autocomplete/base.md

1.9 KiB

Base Autocomplete Implementation

The project uses django-htmx-autocomplete with a custom base implementation to ensure consistent behavior across all autocomplete widgets.

BaseAutocomplete Class

Located in core/forms.py, the BaseAutocomplete class provides project-wide defaults and standardization:

from core.forms import BaseAutocomplete

class MyModelAutocomplete(BaseAutocomplete):
    model = MyModel
    search_attrs = ['name', 'description']

Features

  • Authentication Enforcement: Requires user authentication by default

    • Controlled via AUTOCOMPLETE_BLOCK_UNAUTHENTICATED setting
    • Override auth_check() for custom auth logic
  • Search Configuration

    • minimum_search_length = 2 - More responsive than default 3
    • max_results = 10 - Optimized for performance
  • Internationalization

    • All text strings use Django's translation system
    • Customizable messages through class attributes

Usage Guidelines

  1. Always extend BaseAutocomplete instead of using autocomplete.Autocomplete directly
  2. Configure search_attrs based on your model's indexed fields
  3. Use the AutocompleteWidget with proper options:
class MyForm(forms.ModelForm):
    class Meta:
        model = MyModel
        fields = ['related_field']
        widgets = {
            'related_field': AutocompleteWidget(
                ac_class=MyModelAutocomplete,
                options={
                    "multiselect": True,  # For M2M fields
                    "placeholder": "Custom placeholder..."  # Optional
                }
            )
        }

Performance Considerations

  • Keep search_attrs minimal and indexed
  • Use select_related/prefetch_related in custom querysets
  • Consider caching for frequently used results

Security Notes

  • Authentication required by default
  • Implements proper CSRF protection via HTMX
  • Rate limiting should be implemented at the web server level