[Fixed]-Django admin dropdown of 1000s of users

23👍

The default admin UI displays a dropdown list. Use the raw_id_fields option to get a pop-up window, via a search button. This window lets you find and select the linked object. See the documentation: https://docs.djangoproject.com/en/1.11/ref/contrib/admin/#django.contrib.admin.ModelAdmin.raw_id_fields

By default, Django’s admin uses a select-box interface () for
fields that are ForeignKey. Sometimes you don’t want to incur the
overhead of having to select all the related instances to display in
the drop-down.

5👍

You can look into django-grappelli, which is an app that enhances the admin interface. The documentation describes an autocomplete for ForeignKey or ManyToMany relationships, using raw_id_fields.

👤msc

5👍

You can use django-select2 plugin https://github.com/applegrew/django-select2.

You can do something like:

from django_select2 import AutoModelSelect2Field

class CategoryChoices(AutoModelSelect2Field):
    queryset = models.Category.objects
    search_fields = ['name__icontains', 'code__icontains']

class NewsAdminForm(forms.ModelForm):
    category = CategoryChoices()

    class Meta:
        model = models.News
        exclude = ()

# register in admin
class NewsAdmin(admin.ModelAdmin):
    form = NewsAdminForm
admin.site.register(News, NewsAdmin)

Leave a comment