Simple example of how to use a class based view and django-filter?

后端 未结 2 2007
迷失自我
迷失自我 2021-01-18 12:39

The example in the documentation, https://django-filter.readthedocs.org/en/latest/usage.html, is I think for a function based view. I am currently researching how to do th

相关标签:
2条回答
  • As mentioned in the link Viewing subsets of objects

    You can use something like this is your views.py

    class modelListView(someGenericView):
            queryset = modelName.object.filter(myFilter)
    
    0 讨论(0)
  • 2021-01-18 13:05

    A bit more digging and I have managed to answer it. I have used the code from here https://github.com/rasca/django-enhanced-cbv.

    I added the contents of list.py into my main app as main_app/filter_mixin.py

    Then in the app I was adding a search to the list view I added the file filter.py like this (identical to documentation)

    from django_filters import FilterSet
    from .models import Contact
    
    
    class ContactFilter(FilterSet):
        class Meta:
            model = Contact
            fields = ['name_first', 'name_last']
    

    Now the view.py becomes:

    from vanilla import ListView
    
    from .filter import ContactFilter
    from galleria.filter_mixin import ListFilteredMixin
    
    
    class ContactList(ListFilteredMixin, ListView):
        filter_set = ContactFilter
    
    0 讨论(0)
提交回复
热议问题