Search code examples
djangodjango-formsdjango-admindjango-admin-filters

ModelMultipleChoiceField field returning no value


class DocumentForm(forms.ModelForm):
    model = Document
    starred_by = forms.ModelMultipleChoiceField(queryset=User.objects.all())

    class Meta:
        widgets = {

            'created_by': AutocompleteSelect(
                Document._meta.get_field('created_by').remote_field,
                admin.site,
                attrs={'data-dropdown-auto-width': 'true'}
            ),
            'organisation': AutocompleteSelect(
                Document._meta.get_field('created_by').remote_field,
                admin.site,
                attrs={'data-dropdown-auto-width': 'true'}
            ),
            'starred_by':AutocompleteSelectMultiple(
                Document._meta.get_field('starred_by').remote_field,
                admin.site,
                attrs={'data-dropdown-auto-width': 'true'}
            )
        }

Update:

I have read a bit about the select2 library but I am having trouble understanding how do I integrate it with Django-admin my requirements mostly are searchable select dropdown for foreign keys and multi-select dropdowns for many-to-many fields but these fields I want in the Django admin panel in my apps, any help will be appreciated, thanks in advance


Solution

  • This is how I solved it , I used Django's native search fields which uses Sleect2 under the hood

    class OrganisationAdmin(admin.ModelAdmin):
            list_display = ('name','address')
            autocomplete_fields  = ['products']
            search_fields = ['name__iexact']
            # autocomplete_fields = ['']
        
        
        class ProductAdmin(admin.ModelAdmin):
            search_fields = ['name']