Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: autocomplete filters #1010

Merged
merged 8 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Have you decided to start using Unfold but don’t have time to make the switch
- **Sidebar:** Simplifies the creation of sidebar navigation with icons, collapsibles, and more.
- **Dark mode:** Supports both light and dark mode versions.
- **Actions:** Offers multiple ways to define actions within different parts of the admin interface.
- **Filters:** Custom dropdowns, numeric, datetime, and text fields.
- **Filters:** Custom dropdowns, autocomplete, numeric, datetime, and text fields.
- **Dashboard:** Includes helpers for creating custom dashboard pages.
- **Components:** Reusable UI components such as cards, buttons, and charts.
- **WYSIWYG widget:** Built-in support for WYSIWYG (Trix).
Expand Down
37 changes: 37 additions & 0 deletions docs/filters/autocomplete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
title: Autocomplete filter
order: 5
description: Autocomplete filters for changelist view.
---

# Autocomplete filters

Unfold provides two different types of autocomplete filters: `AutocompleteSelectFilter` and `AutocompleteSelectMultipleFilter`. Both of them are implemented in `unfold.contrib.filters` so make sure this app is in your `INSTALLED_APPS` in `settings.py`.

All the referenced fields must be `ForeignKey` or `ManyToManyField` fields and the same time the referenced admin model must have defined `search_fields` attribute otherwise the application will raise an error.

```python
# admin.py

from django.contrib import admin
from django.contrib.auth.models import User

from unfold.admin import ModelAdmin
from unfold.contrib.filters.admin import (
AutocompleteSelectFilter,
AutocompleteSelectMultipleFilter
)

@admin.register(User)
class YourModelAdmin(ModelAdmin):
list_filter = (
# Autocomplete filter
["other_model_field", AutocompleteSelectFilter],

# Autocomplete multiple filter
["other_multiple_model_field", AutocompleteSelectMultipleFilter],
)

class OtherModelAdmin(ModelAdmin):
search_fields = ["name"]
```
Loading