Добавлены фильтры для списка клиентов через django-filter
- Создан CustomerFilter с тремя фильтрами: * Есть заметки (has_notes) * Нет телефона (no_phone) * Нет email (no_email) - Обновлен views.py для использования фильтров - Добавлены чекбоксы фильтров в шаблон списка клиентов - Фильтры работают совместно с поиском - Кнопка Очистить отображается при активных фильтрах или поиске
This commit is contained in:
61
myproject/customers/filters.py
Normal file
61
myproject/customers/filters.py
Normal file
@@ -0,0 +1,61 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Фильтры для клиентов с использованием django-filter
|
||||
"""
|
||||
|
||||
import django_filters
|
||||
from django import forms
|
||||
from .models import Customer
|
||||
|
||||
|
||||
class CustomerFilter(django_filters.FilterSet):
|
||||
"""
|
||||
Фильтр для списка клиентов
|
||||
Поддерживает фильтрацию по:
|
||||
- Наличию заметок
|
||||
- Отсутствию телефона
|
||||
- Отсутствию email
|
||||
"""
|
||||
|
||||
# Фильтр: есть заметки
|
||||
has_notes = django_filters.BooleanFilter(
|
||||
method='filter_has_notes',
|
||||
label='Есть заметки',
|
||||
widget=forms.CheckboxInput(attrs={'class': 'form-check-input'})
|
||||
)
|
||||
|
||||
# Фильтр: нет телефона
|
||||
no_phone = django_filters.BooleanFilter(
|
||||
method='filter_no_phone',
|
||||
label='Нет телефона',
|
||||
widget=forms.CheckboxInput(attrs={'class': 'form-check-input'})
|
||||
)
|
||||
|
||||
# Фильтр: нет email
|
||||
no_email = django_filters.BooleanFilter(
|
||||
method='filter_no_email',
|
||||
label='Нет email',
|
||||
widget=forms.CheckboxInput(attrs={'class': 'form-check-input'})
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = Customer
|
||||
fields = ['has_notes', 'no_phone', 'no_email']
|
||||
|
||||
def filter_has_notes(self, queryset, name, value):
|
||||
"""Фильтр клиентов с заметками"""
|
||||
if value:
|
||||
return queryset.filter(notes__isnull=False).exclude(notes='')
|
||||
return queryset
|
||||
|
||||
def filter_no_phone(self, queryset, name, value):
|
||||
"""Фильтр клиентов без телефона"""
|
||||
if value:
|
||||
return queryset.filter(phone__isnull=True) | queryset.filter(phone='')
|
||||
return queryset
|
||||
|
||||
def filter_no_email(self, queryset, name, value):
|
||||
"""Фильтр клиентов без email"""
|
||||
if value:
|
||||
return queryset.filter(email__isnull=True) | queryset.filter(email='')
|
||||
return queryset
|
||||
Reference in New Issue
Block a user