mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 14:11:09 -05:00
1.9 KiB
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_UNAUTHENTICATEDsetting - Override
auth_check()for custom auth logic
- Controlled via
-
Search Configuration
minimum_search_length = 2- More responsive than default 3max_results = 10- Optimized for performance
-
Internationalization
- All text strings use Django's translation system
- Customizable messages through class attributes
Usage Guidelines
- Always extend
BaseAutocompleteinstead of usingautocomplete.Autocompletedirectly - Configure search_attrs based on your model's indexed fields
- 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_attrsminimal and indexed - Use
select_related/prefetch_relatedin 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