问题描述
我正在学习 Django,并开始创建一个 Web 应用程序,并尝试将 django_filters 与 django_tables2 一起使用,以过滤表中的列。根据 django_filters 说明,我想要更改的是默认的“精确”查找方法。这是他们在 FilterSet Options
展示的示例class UserFilter(django_filters.FilterSet):
class Meta:
model = User
fields = {
'username': ['exact','contains'],'last_login': ['exact','year__gt'],}
结果是,如果我不在查找列表中包含“精确”(例如下面的 shop__shop 字段),则该字段不会呈现在页面上。
class ReceiptFilter(django_filters.FilterSet):
class Meta:
model = expenses
fields = {
'purchase_date': ['exact'],'shop__shop': ['iexact'],'payment_method__payment_method': ['exact'],}
Please,click here to see the web page rendered
如果我在要添加的查找之前保留“精确”(如说明中所述),它似乎没有任何效果,过滤器就像是“精确”查找一样工作。
我做错了什么?
谢谢, 三木。
型号:
class shops(models.Model):
shop = models.CharField(max_length=50,null=True)
address = models.CharField(max_length=100,null=True)
shop_type = models.CharField(max_length=50,null=True)
phone = models.CharField(max_length=50,null=True)
def __str__(self):
return self.shop
class Meta:
verbose_name = "Shop"
class expenses(models.Model):
item = models.ForeignKey(items,on_delete=models.CASCADE)
shop = models.ForeignKey(shops,on_delete=models.CASCADE)
owner = models.ForeignKey(owners,on_delete=models.CASCADE)
currency = models.ForeignKey(currencies,on_delete=models.CASCADE)
payment_method = models.ForeignKey(payment_methods,on_delete=models.CASCADE)
price = models.DecimalField(max_digits=10,decimal_places=2)
amount = models.DecimalField(max_digits=10,decimal_places=3)
purchase_date = models.DateField()
entry_time = models.DateTimeField(null=True)
exclude_from_reports = models.BooleanField(null=True)
transferred = models.BooleanField(null=True)
def __str__(self):
return str(self.amount) + ' ' + self.item.unit + ' of ' + self.item.item
class Meta:
verbose_name = "Expense"
表格:
class ReceiptsTable(tables.Table):
class Meta:
model = expenses
template_name = "django_tables2/bootstrap.html"
fields = ('purchase_date','shop__shop','payment_method__payment_method','currency__currency_short','total')
查看:
def receipts(request):
receipt_list=expenses.objects.values('purchase_date','currency__currency_short').annotate(total=Sum('price')).order_by('-purchase_date')
filter = ReceiptFilter(request.GET,queryset=receipt_list)
return render(request,'household/receipts.html',{'filter':filter})
模板:
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'household/style.css' %}">
{# <a href='/household/add_expense'>Add Expense</a> #}
{% block content %}
<table>
<caption>Receipts</caption>
<thead>
<tr>
<th>Date</th>
<th>Shop</th>
<th>Payment Method</th>
<th>Total</th>
</tr>
</thead>
<thead>
<form method='get'>
<tr>
<th>{{ filter.form.purchase_date }}</th>
<th>{{ filter.form.shop__shop }}</th>
<th>{{ filter.form.payment_method__payment_method }}</th>
<th><input type='submit' value='Filter'/></th>
</tr>
</form>
</thead>
<tbody>
{% for obj in filter.qs %}
<tr>
<td>{{ obj.purchase_date }}</td>
<td>{{ obj.shop__shop }}</td>
<td>{{ obj.payment_method__payment_method }}</td>
<td>{{ obj.currency__currency_short }} {{ obj.total }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)