【发布时间】:2021-10-01 20:38:07
【问题描述】:
我有一个字段profit_loss_value_fees,我希望用户在这个字段中有一个下拉选项来显示所有盈利/亏损/临时交易。
Profit = Trade.profit_loss_value_fees > 0
Loss = Trade.profit_loss_value_fees < 0
Scratch = Trade.profit_loss_value_fees == 0
我所做的是一个result1,它可以工作,但我没有得到一个很好的下拉菜单,如上所述。这是两个空白字段,用户可以在其中输入 1 和 100,然后显示该范围内的所有交易。它可以工作,但不是所需的外观。
filters.py
class StatsPortfolioFilter(django_filters.FilterSet):
# Drop down with 3 options (Win, Loss, Scratch) using the Trade Field: profit_loss_value_fees
result1 = django_filters.RangeFilter(
field_name='profit_loss_value_fees',
label='result1',
)
我尝试使用 LookupChoiceFilter 创建 3 个下拉选项,并且效果很好。问题是选项显然没有做任何事情。如何添加
filters.py
class StatsPortfolioFilter(django_filters.FilterSet):
# I need something that gives the lookup_choices value
# w = Trade.profit_loss_value_fees > 0
# l = Trade.profit_loss_value_fees < 0
# s = Trade.profit_loss_value_fees == 0
# Drop down with 3 options (Win, Loss, Scratch) using the Trade Field: profit_loss_value_fees
result = django_filters.LookupChoiceFilter(
field_name='profit_loss_value_fees', # field_name focuses on field relatetion
label='Result',
# field_class=forms.DecimalField,
lookup_choices=[
('w', 'Win'),
('l', 'Loss'),
('s', 'Scratch'),
]
)
目前还没有错误消息,因为我不知道如何继续。
【问题讨论】:
标签: django filter django-forms django-filter