Adding external widget (django-select2) to django-filters

1,059 views
Skip to first unread message

Mariusz K

unread,
Mar 11, 2015, 9:00:32 AM3/11/15
to django...@googlegroups.com
I want to add a nice selector for the users. I use django-filter and want to add a library django-selector2.
I have something like this, I think it does not work as it should - it appears normal selector.
It seems that django-filters do not use a form with a new field selection.


from django_select2 import *


class UserChoices(ModelSelect2MultipleField):
    queryset
= FreeDay.objects
    search_fields
= ['word__icontains', ]


class FreeDaysForm(InlineForm):
    user
= UserChoices()

   
class Meta:
        model
= FreeDay



class FreeDaysFilter(django_filters.FilterSet):
    blank_choice
= (('', ugettext_lazy('---')),)
    date_from
= django_filters.DateFilter(widget=DateInput(attrs={'class':'datepicker'}))
    date_to
= django_filters.DateFilter(widget=DateInput(attrs={'class':'datepicker'}))


   
class Meta:
        model
= FreeDay
        form
= FreeDaysForm
        fields
= ['user', 'country', 'date_from', 'date_to']






My view:

class DashboardView(TableFilterView):
   
"""
    Displays a list and filter on the main page.
    """

    model
= FreeDay
    table_class
= FreeDaysTable
    template_name
= "freedays/list_of_freedays.html"
    context_object_name
= 'free_days'
    filterset_class
= FreeDaysFilter


   
def get_context_data(self, **kwargs):
        context
= super(DashboardView, self).get_context_data(**kwargs)
        f
= FreeDaysForm()
        context
['f'] = f
       
return context

In the template I have:

{% load django_select2_tags %} {% import_django_select2_js %} {% import_django_select2_css %} {% import_django_select2_js_css light=1 %}

Carlton Gibson

unread,
Mar 11, 2015, 9:29:34 AM3/11/15
to django...@googlegroups.com
Hi Mariusz, 

I’m not sure about the `django-select2` package but I’ve successfully used vanilla `select2` with a Django Filter. 

1. Define you Filter as normal, using a MultipleChoiceFilter. 
2. Echo the filter form (as per normal).
3. Then in JavaScript attach this in the page:

    $('select#id_free_days').select2({
        placeholder: "Select your free days"
    })

… or such. 

Hopefully that helps. 

Regards,

Carlton

--
You received this message because you are subscribed to the Google Groups "django-filter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-filte...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mariusz K

unread,
Mar 11, 2015, 11:04:00 AM3/11/15
to django...@googlegroups.com
Hello Carlton,
Thank you for your reply.
In the project I used this solution: https://github.com/applegrew/django-select2
You also have used it?

Carlton Gibson

unread,
Mar 11, 2015, 1:00:19 PM3/11/15
to django...@googlegroups.com
No sorry, I haven't. What does it add to just using select2 on its own?

Sent from my iPhone
Reply all
Reply to author
Forward
0 new messages