Giter VIP home page Giter VIP logo

django-generic-filters's Introduction

django-generic-filters

django-generic-filters is a toolkit to filter results of Django's ListView, using forms.

Main use cases are obviously search forms and filtered lists.

As a developer, given you have a ListView, in order to let the user filter the results:

  • use a form to easily render the filters as HTML;
  • the user typically sends the filters via GET;
  • validate the user's input using a Django form;
  • filter the Django view's queryset using form's cleaned data.

Build Status

Example

views.py

from django_genericfilters.views import FilteredListView


class UserListView(FilteredListView):
    # ListView options. FilteredListView inherits from ListView.
    model = User
    template_name = 'user/user_list.html'
    paginate_by = 10
    context_object_name = 'users'

    # FormMixin options. FilteredListView inherits from FormMixin.
    form_class = UserListForm

    # FilteredListView options.
    search_fields = ['first_name', 'last_name', 'username', 'email']
    filter_fields = ['is_active', 'is_staff', 'is_superuser']
    default_order = 'last_name'

    def form_valid(self, form):
        """Return the queryset when form has been submitted."""
        queryset = super(UserListView, self).form_valid(form)

        # Handle specific fields of the custom ListForm
        # Others are automatically handled by FilteredListView.

        if form.cleaned_data['is_active'] == 'yes':
            queryset = queryset.filter(is_active=True)
        elif form.cleaned_data['is_active'] == 'no':
            queryset = queryset.filter(is_active=False)

        if form.cleaned_data['is_staff'] == 'yes':
            queryset = queryset.filter(is_staff=True)
        elif form.cleaned_data['is_staff'] == 'no':
            queryset = queryset.filter(is_staff=False)

        if form.cleaned_data['is_superuser'] == 'yes':
            queryset = queryset.filter(is_superuser=True)
        elif form.cleaned_data['is_superuser'] == 'no':
            queryset = queryset.filter(is_superuser=False)

        return queryset

forms.py

from django import forms
from django.utils.translation import gettext_lazy as _
from django_genericfilters import forms as gf


class UserListForm(gf.QueryFormMixin, gf.OrderFormMixin, gf.FilteredForm):
    is_active = gf.ChoiceField(label=_('Status'),
                               choices=(('yes', _('Active')),
                                        ('no', _('Unactive'))))

    is_staff = gf.ChoiceField(label=_('Staff'))

    is_superuser = gf.ChoiceField(label=_('Superuser'))

    def get_order_by_choices(self):
        return [('date_joined', _(u'date joined')),
                ('last_login', _(u'last login')),
                ('last_name', _(u'Name'))]

Forms

Several form mixins are provided to cover frequent use cases:

  • OrderFormMixin with order_by and order_reverse fields.
  • QueryFormMixin for little full-text search using icontains.

See "mixin" documentation for details.

Ressources

django-generic-filters's People

Contributors

benoitbryon avatar boblefrag avatar brunobord avatar daindwarf avatar ewjoachim avatar greatwizard avatar hobbestigrou avatar joehybird avatar k4nar avatar mgu avatar mike-perdide avatar moumoutte avatar natim avatar pleasedontbelong avatar samuelhamard avatar tmartinfr avatar wo0dyn avatar yakuru avatar zebuline avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

django-generic-filters's Issues

add a get_qs_filter to FilteredView

FilteredView should be able to get a standard list of filter fields via the filter_fields parameter.

One should be able to override this method to implement his own list of filters.

Handle CheckBox

We need to display checkboxes right next to their labels.

Documentation? purpose of the QueryFormMixin

If queryformmixin only exists to attach a hidden text input field on the form, which was quite confusing at first, why is it setup like this?

I have to not use it in order to do any queries unless i want to un-hide the query input on the client using javascript. It just seem so contradictory. Is there some explanation why its setup like this? Otherwise I may write a pull request to clean it up.

Bootstrap 5 Support?

Hello, i was wondering if there a plans to add support for Bootstrap 5? Bootstrap 5 doesn't use jquery, so JS there might be an overhaul in the JS for this support.

Code refactoring on forms.py

remove the get_hidden_field method and set the widget type in the revelant mixins.

Mixins should not inherit from forms.Form but from object (otherwise it's not Mixin anymore)

Milestones in Github

Unless otherwise specified, I will setup the following milestones to manage issues in the bugtracker:

  • have a X.X (example: 0.9) milestone for features we merge in master. So that we can get the list of tickets for each release.
  • have a "soon" milestone, where we put tickets with either high priority or simple changes: tickets that are candidates for the next milestone. As an example features/bugs that are must-have.
  • have a "later" milestone, where we put tickets with lower priority. As an example features that are nice-to-have or tickets that are too big for a near future.
  • have a "maybe, one day" milestone, where we put ideas we'd like to implement, but that we do not plan to implement soon. As an example features that involve really big changes, or significant API changes... This milestone would participate to vision, a.k.a. the long term roadmap.

I use this scheme in several projects, such as:

... and I like it ;)

CHANGELOG and README also mention the "roadmap". Example: https://github.com/diecutter/diecutter/blob/9d9066a17dde6cd42cea4301ba576ed669ef7ed1/README.rst (end of file) and https://github.com/diecutter/diecutter/blob/9d9066a17dde6cd42cea4301ba576ed669ef7ed1/CHANGELOG#L197

I can publish an article on this topic on our Novapost blog, then reference this article in the CONTRIBUTING file.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.