Add autocomplete functionality for parks: implement BaseAutocomplete class and integrate with forms

This commit is contained in:
pacnpal
2025-02-22 13:36:24 -05:00
parent 5278ad39d0
commit 4339c5c5e0
15 changed files with 633 additions and 130 deletions

View File

@@ -0,0 +1,63 @@
# 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:
```python
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:
```python
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