pythondjangodjango-adminjquery-select2django-select2

How to use django-select2 widgets in django admin site?


In my django app I have modified the User entity to include a worker field (OneToOneField). But from the django admin site that field yiels so many result , so it is difficult for the logged user to select a worker. Is there any way to use the select2 (ModelSelect2Widget) widgets from the django admin site? For any regular form I have define the widgets in the following way:

from django_select2.forms import ModelSelect2Widget
    class ProcessForm(forms.ModelForm):
        class Meta:
            model = ProcessModel
            exclude = ('id',)
    
            widgets = {
                'name':forms.TextInput(attrs={'class': 'form-control'}),
                'code':forms.TextInput(attrs={'class': 'form-control'}),
                'description':forms.Textarea(attrs={'class': 'form-control'}),
                'geom': LeafletWidget(),
                'product': ModelSelect2Widget(model=ProductModel, queryset=ProductModel.objects.filter(),
                                                search_fields=['name__icontains'],
                                                attrs={'style': 'width: 100%;'}), 
    }

Is there any way to use the ModelSelect2Widget for the worker field in the admin site form? Here is my code:

class User(AbstractUser):
    worker = models.OneToOneField(WorkerModel, on_delete=models.CASCADE, 
                related_name="user", verbose_name=_("Trabajador"), null=True, blank=True)

    
    class Meta:
        default_permissions = ()
        verbose_name="Usuario"
        verbose_name_plural="Usuarios"
        permissions = (
            ("secretario", "Secretario(a)"),
            ("director", "Director"),
        
        )
from django.contrib.auth.admin import UserAdmin

class UserAdminInherited(UserAdmin):
    fieldsets = (
        (None, {'fields': ('username', 'password')}),
        (_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
        (_('Worker info'), {'fields': ('worker',)}),
        (_('Permissions'), {
            'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions'),
        }),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
    )

admin.site.register(User, UserAdminInherited)

Solution

  • Since Django 2.0., we can use autocomplete_fields.

    autocomplete_fields is a list of ForeignKey and/or ManyToManyField fields you would like to change to Select2 autocomplete inputs.

    ModelAdmin(is parent of UserAdmin) has the property autocomplete_fields (Django Docs):

    from django.contrib.auth.admin import UserAdmin
    from django.contrib.auth import get_user_model
    
    
    User = get_user_model()
    
    
    class UserAdminInherited(UserAdmin):
        autocomplete_fields = ['worker']
        ...
    
    
    admin.site.register(User, UserAdminInherited)
    

    You must define search_fields on the related object’s ModelAdmin because the autocomplete search uses it.

    from django.contrib import admin
    
    @admin.register(WorkerModel)
    class WorkerModelAdmin(admin.ModelAdmin):
        search_fields = ['model_field']