【发布时间】:2021-04-21 08:36:06
【问题描述】:
我有模特
class TransHeader(models.Model):
th_type = models.CharField(max_length=200)
th_code = models.CharField(max_length=200)
th_status=models.CharField(max_length=1,default=0)
我的看法:
class SalTransactions(TemplateView):
template_name = "wstore/sales_transactions.html"
def get_context_data(self, **kwargs):
context = {}
if kwargs['post']=='UNPOSTED':
context['transactions'] =
TransHeader.objects.filter(th_type=kwargs['transtype'])
.filter(th_status='0')
else:
context['transactions'] =
TransHeader.objects.filter(th_type=kwargs['transtype'])
return context
我的网址行:
path('saltransactions/<str:transtype>/<str:post>',views.SalTransactions.as_view(), name='saltransactions'),
我的菜单链接工作正常,显示给定 trans 类型的所有记录:
<a class="nav-link" href="{% url 'saltransactions' transtype='INV' post='ALL' %}">Sales</a>
<a class="nav-link" href="{% url 'saltransactions' transtype='ADJ' post='ALL' %}">Stock Adjustment</a>
但是当我过滤以显示仅具有 th_status='0' 的 rocords 时,我在模板中使用以下链接:
<a href="{% url 'saltransactions' transtype=?????" post='UNPOSTED' %}" >Unposted</a>
我的问题是如何通过 INV 或 ADJ 从查询集中进行转换。如果我分配 transtype='INV' 它正在过滤两种类型的 INV 查询集。
这样我就可以对所有类型的交易(INV/ADJ)使用一个视图、一个模板
感谢您的帮助
【问题讨论】:
标签: python django templates filter django-queryset