The Wayback Machine - https://web.archive.org/web/20200709202649/https://github.com/awesto/django-shop/issues/573
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement docs about filters #573

Open
vivazzi opened this issue Apr 27, 2017 · 1 comment
Open

Improvement docs about filters #573

vivazzi opened this issue Apr 27, 2017 · 1 comment

Comments

@vivazzi
Copy link
Contributor

@vivazzi vivazzi commented Apr 27, 2017

I find better code about filters than in docs which allow write less code when we declare fields.
Instead of (see http://django-shop.readthedocs.io/en/latest/reference/filters.html#id3):

from django.forms import forms, widgets
import django_filters
from djng.forms import NgModelFormMixin
from myshop.models.product import MyProduct, Manufacturer

class FilterForm(NgModelFormMixin, forms.Form):
    scope_prefix = 'filters'

class ProductFilter(django_filters.FilterSet):
    manufacturer = django_filters.ModelChoiceFilter(
        queryset=Manufacturer.objects.all(),
        widget=Select(attrs={'ng-change': 'filterChanged()'}),
        empty_label="Any Manufacturer")

    class Meta:
        model = MyProduct
        form = FilterForm
        fields = ['manufacturer']

    @classmethod
    def get_render_context(cls, request, queryset):
        """
        Prepare the context for rendering the filter.
        """
        filter_set = cls()
        # we only want to show manufacturers for products available in the current list view
        filter_field = filter_set.filters['manufacturer'].field
        filter_field.queryset =filter_field.queryset.filter(
            id__in=queryset.values_list('manufacturer_id'))
        return dict(filter_set=filter_set)

I suggest:

...

class FilterForm(NgModelFormMixin, Bootstrap3Form):
    def __init__(self, *args, **kwargs):
        kwargs.update({'scope_prefix': 'filters',
                       'ng_change': 'filterChanged()'})
        super(FilterForm, self).__init__(*args, **kwargs)

class ProductFilter(django_filters.FilterSet):
    manufacturer = django_filters.ModelChoiceFilter(
        queryset=Manufacturer.objects.all(),
        empty_label="Any Manufacturer")

    ...

Other words I changed FilterForm and remove widget=Select(attrs={'ng-change': 'filterChanged()'}), to we don't need write it every time.

I suggest to fix django shop documentation about filters, adding more information about how django-shop auto adds attributes ng-model and ng-change to html inputs. Also I suggest to add this information to http://django-angular.readthedocs.io/en/latest/angular-model-form.html (Excuse me if I miss information about it).

P. S. If you agree with me I can add pull request.

@jrief
Copy link
Member

@jrief jrief commented Aug 25, 2017

agree on that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants
You can’t perform that action at this time.