Django-Filter:为Boolean字段创建复选框

时间:2018-03-28 10:29:11

标签: python django django-filter

Models.py

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Application.ScreenUpdating = False
    Application.EnableEvents = False

    Select Case Target.Address

        Case "$G$6"
            If InStr(1, Range("G6"), "PUMP") > 0 Then
                MsgBox ("Pump")
            ElseIf InStr(1, Range("G6"), "SKID") > 0 Then
                MsgBox ("Skid")
            End If

        Case "$G$24:$H$54"
        If Not Application.Intersect(Target, Range("G24:H54")) Is Nothing Then
            If InStr(1, Range("G24"), "Calculate") > 0 And InStr(1, Range("G25"), "Outside Shelter") > 0 Then
                Cells(19, 8).Value = Sheets("1").Cells(159, 6).Value
                Cells(20, 9).Value = Sheets("1").Cells(163, 6).Value
                Cells(19, 11).Value = Sheets("1").Cells(160, 6).Value
                Cells(20, 10).Value = Sheets("1").Cells(164, 6).Value
            ElseIf InStr(1, Range("G24"), "Calculate") > 0 And InStr(1, Range("G25"), "Inside Shelter") > 0 Then
                Cells(19, 8).Value = Sheets("1").Cells(182, 6).Value
                Cells(20, 9).Value = Sheets("1").Cells(187, 6).Value
                Cells(19, 11).Value = Sheets("1").Cells(183, 6).Value
                Cells(20, 10).Value = Sheets("1").Cells(188, 6).Value
            End If
        End If

        Application.EnableEvents = True
        Application.ScreenUpdating = True

    End Select
End Sub

我想使用django-filter在表单中创建以下复选框:

[]在线 []物理

如果表单为空或两者都勾选,请获取class Task(models.Model): online = models.BooleanField(blank=False) 。如果只勾选了一个,那么请Task.objects.all()

我尝试添加以下内容:

Task.objects.filter('Online'=True/False)

我尝试了以下小部件:import django_filters from app.models import Task from django.db import models class TaskFilter(django_filters.FilterSet): online = django_filters.BooleanFilter(name='online', lookup_expr='isnull') class Meta: model = Task fields = ['online'] filter_overrides = { models.BooleanField: { 'filter_class': django_filters.BooleanFilter, 'extra': lambda f: { 'widget': forms.CheckboxInput, }, }, } ,但似乎我得到了所有这些小部件的相同输出。我得到一个下拉列表,例如

在线:[下拉选择是,否,未知的值]

2 个答案:

答案 0 :(得分:1)

你可以选择那种东西:

TYPE_CHOICES = (
    (0, 'Online'),
    (1, 'Physical'),
)
class Task(models.Model):
    type = models.CharField(
        choices=TYPE_CHOICES, default=0, max_length=100
    )
    objects = TaskQuerySet().as_manager()

然后你可以像往常一样过滤它:

Task.objects.filter(type=0).filter(...)

为了使您更容易添加自定义查询集类:

class TaskQuerySet(models.QuerySet):
    def get_online(self):
        return self.filter(type=0)

    def get_physical(self):
        return self.filter(type=1)

这将允许您执行以下操作:

Task.objects.get_online.filter(...)
Task.objects.get_physical.filter(...)

filters.py 中添加:

type = django_filters.MultipleChoiceFilter(field_name='type', choices=CHOICES, widget=forms.CheckboxSelectMultiple)

答案 1 :(得分:0)

过滤器接受小部件参数,因此,如果您要手动实例化过滤器,则可以使用:

class TaskFilter(django_filters.FilterSet):
    online = django_filters.filters.BooleanFilter(widget=forms.CheckboxInput)
    class Meta:
        model = Task
        fields = ['online']

但是,如果您使用元类声明字段,则可以使用filter_overrides

覆盖行为。
class TaskFilter(django_filters.FilterSet):
    filter_overrides = {
        models.BooleanField: {
            'filter_class': filters.BooleanFilter,
            'extra': lambda f: {
                widget: forms.CheckboxInput
            },
        }
    }
    class Meta:
        model = MyModel
        fields = ['online']

尽管经过大量测试,但我得出的结论通常对default=False模型字段很有帮助。

refer: django-filter issue