mark
mark

Reputation: 673

Connection django-select2 with django-filters

I want to connect with select2 django with django-filters. I want to have a nice selector to select users. I do not know where to put the following code in the code django-filters.

from django_select2 import *

class UserChoices(AutoModelSelect2Field):
    queryset = User.objects
    search_fields = ['word__icontains', ]

Upvotes: 1

Views: 2478

Answers (1)

creimers
creimers

Reputation: 5305

Here's how I did it, sticking to the django-filter example and using django-select2:

import django_filters
from django_select2.widgets import Select2Widget
from .models import Product

class ProductFilter(django_filters.FilterSet):
    name = django_filters.ModelChoiceFilter(
        queryset=Product.objects.all(),
        widget=Select2Widget
    )
    class Meta:
        model = Product
        fields = ['name', 'price', ]

Make sure you have jquery in your (base) template. Then this is what you'll get:

enter image description here

Upvotes: 1

Related Questions