如何从使用django_filters创建的BooleanField过滤器的下拉列表中删除或更改'Unknown'选项?

wyyhbhjk  于 2023-06-25  发布在  Go
关注(0)|答案(1)|浏览(132)

在我的models.py中,有一个名为cashBooleanField

class Realization(models.Model):
    patient=models.ForeignKey(Patient, on_delete=CASCADE)
    patient_type=models.ForeignKey(PatientType, on_delete=CASCADE, default=None)
    cash=models.BooleanField(default=False)
    amount_received=models.DecimalField(max_digits=10, decimal_places=2, default=0)

我将这个model用作另一个modelIPDReport中的ForeignKey

class IpdReport(models.Model):
    patient=models.ForeignKey(Patient, on_delete=CASCADE)
    package=models.ForeignKey(Package, on_delete=CASCADE, blank=True, null=True)
    approvedpackage=models.ForeignKey(ApprovedPackage, on_delete=CASCADE, blank=True, null=True)
    ctscan=models.ForeignKey(CTScan, on_delete=CASCADE, blank=True, null=True)
    radiations=models.ForeignKey(Radiations, on_delete=CASCADE, blank=True, null=True)
    discharge=models.ForeignKey(Discharge, on_delete=CASCADE, blank=True, null=True)
    realization=models.ForeignKey(Realization, on_delete=CASCADE, blank=True, null=True)
    lockdata=models.ForeignKey(LockData, on_delete=CASCADE, blank=True, null=True)

我想为IPDReport数据创建一个过滤器,包括Realization's cash字段,所以我使用了这种方法:

class IpdFilters(django_filters.FilterSet):
    patient__name=django_filters.CharFilter(widget=forms.TextInput(attrs={'class':'form-control'}), lookup_expr='icontains', label='Name')
    ctscan__rt_number=django_filters.CharFilter(widget=forms.TextInput(attrs={'class':'form-control'}), lookup_expr='icontains', label='RT Number')
    package__patient_type__patient_type=django_filters.CharFilter(widget=forms.TextInput(attrs={'class':'form-control'}), lookup_expr='icontains', label='Type of Patient')
    realization__amount_received__gt=django_filters.NumberFilter(widget=forms.NumberInput(attrs={'class':'form-control'}), field_name='realization__amount_received', lookup_expr='gt', label='Min. Amount Received')
    realization__amount_received__lt=django_filters.NumberFilter(widget=forms.NumberInput(attrs={'class':'form-control'}), field_name='realization__amount_received', lookup_expr='lt', label='Max. Amount Received')
    from_date=django_filters.DateFilter(widget=forms.DateInput(attrs={'id': 'from_doa', 'class':'form-control'}), field_name='package__date_of_admission', label='From - Date of Admission ', lookup_expr='gte')
    to_date=django_filters.DateFilter(widget=forms.DateInput(attrs={'id': 'to_doa', 'class':'form-control'}), field_name='package__date_of_admission', label='To - Date of Admission ', lookup_expr='lte')
    from_date_bill=django_filters.DateFilter(widget=forms.DateInput(attrs={'id': 'from_bd', 'class':'form-control'}), field_name='realization__billing_month', label='From - Date of Billing ', lookup_expr='gte')
    to_date_bill=django_filters.DateFilter(widget=forms.DateInput(attrs={'id': 'to_bd', 'class':'form-control'}), field_name='realization__billing_month', label='To - Date of Billing ', lookup_expr='lte')
    
    class Meta:
        model=IpdReport
        fields={
            'realization__cash':['exact']
        }

现在,在我的应用程序中的IPDReport页面上,该字段呈现如下:

在下拉列表中,默认选项显示为“未知”。有什么办法可以完全改变或删除它吗?
另外,如何在Meta class下的现金字段中添加HTML class attribute

os8fio9y

os8fio9y1#

Django Filter的BooleanWidget继承自Django的Select widget,后者继承自ChoiceWidget。
不幸的是,BooleanWidget.__init__不接受choices参数,因此您不能简单地覆盖BooleanWidget.__init__并调用super().__init__。我能够通过在超级链中完全跳过BooleanWidget.__init__来解决这个问题。关于向表单字段添加类,这在Django中是非常标准的,Django Filter不会混淆此功能。我把它包含在下面的解决方案中。

from django import forms

import django_filters
from django_filters.widgets import BooleanWidget

class CustomBooleanWidget(BooleanWidget):
    def __init__(self, attrs=None, choices=()):
        # remove the 'unknown' option here
        choices = (('true', 'Yes'), ('false', 'No'))

        # bypass BooleanWidget.__init__
        forms.Select.__init__(self, attrs, choices)

class IpdFilters(django_filters.FilterSet):
    # ...
    realization__cash = django_filters.BooleanFilter(<desired attributes>, widget=CustomBooleanWidget(attrs={'class': '<desired class names>'})

相关问题