Skip to content
Open
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
30 changes: 27 additions & 3 deletions docs/fields/autocomplete.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,38 @@ class MyModelAdmin(ModelAdmin):
class MyForm(forms.Form):
one_object = UnfoldAdminAutocompleteModelChoiceField(
label=_("Object - Single value"),
# Important for validation. Make sure it offers same results as the custom view
queryset=MyModel.objects.all(),
# Create here a limited queryset to avoid having to render all results at once
queryset=MyModel.objects.all()[0:20],
# Map autocomplete results to the custom view
url_path="admin:custom_autocomplete_path_name",
)
multiple_objects = UnfoldAdminMultipleAutocompleteModelChoiceField(
label=_("Objects - Multiple values"),
queryset=MyModel.objects.all(),
# Create here a limited queryset to avoid having to render all results at once
queryset=MyModel.objects.all()[0:20],
url_path="admin:custon_autocomplete_path_name",
)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

# In case the form contains initial values, you want to add these to the queryset
if self.initial.get("one_object"):
self.fields["one_object"].queryset = self.fields["one_object"].queryset.filter(
pk=self.initial.get("one_object")
)
if self.initial.get("multiple_objects"):
self.fields["multiple_objects"].queryset = self.fields["multiple_objects"].queryset.filter(
pk__in=self.initial.get("multiple_objects")
)


# Important - Validate the queryset when the form is submitted
if request.method == "POST":
self.fields["one_object"].queryset = MyModel.objects.filter(
pk__in=request.POST.getlist("one_object")
)
self.fields["multiple_objects"].queryset = MyModel.objects.filter(
pk__in=request.POST.getlist("multiple_objects")
)
```
2 changes: 1 addition & 1 deletion src/unfold/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> JsonResponse:
"id": obj.pk,
"text": str(obj),
}
for obj in self.object_list
for obj in context["page_obj"]
],
"pagination": {
"more": context["page_obj"].has_next(),
Expand Down