在我的models.py
中,有一个名为cash
的BooleanField
:
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
用作另一个model
IPDReport
中的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
?
1条答案
按热度按时间os8fio9y1#
Django Filter的BooleanWidget继承自Django的Select widget,后者继承自ChoiceWidget。
不幸的是,
BooleanWidget.__init__
不接受choices
参数,因此您不能简单地覆盖BooleanWidget.__init__
并调用super().__init__
。我能够通过在超级链中完全跳过BooleanWidget.__init__
来解决这个问题。关于向表单字段添加类,这在Django中是非常标准的,Django Filter不会混淆此功能。我把它包含在下面的解决方案中。