0

I need to override the way a Select2 widget of a particular ForeignKey is behaving in the Django (4.1) admin.

Namely, I am trying to increase the number of objects returned by the AJAX requests and defined by paginate_by in AutocompleteJsonView.

Sadly this great solution no longer works with Django 4.

How can I extend AutocompleteJsonView and somehow tell Django to use my custom view?

EDIT: my current workaround is to override get_paginator on the ModelAdmin by setting paginator.per_page to whatever value is suitable, but that doesn't answer to broader question of customising the autocomplete behaviour of Select2 widgets in the Django admin.

1 Answer 1

0

Here is what I did for this after upgrading from Django 2.2 to 3.2, since autocomplete_view moved from Admin to Site.

from django.contrib.admin.views.autocomplete import AutocompleteJsonView class MySite(AdminSite): def autocomplete_view(self, request): if request.GET['model_name'] == "mymodel": return CustomAutocompleteJsonView.as_view(admin_site=self)(request) else: return super().autocomplete_view(request) class CustomAutocompleteJsonView(AutocompleteJsonView): pass # Override serialize_result, get, or get_paginator here 

(That being said, it might be a better idea to implement and use a custom api instead of trying to re-use this undocumented class that could change again any time.)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.