[英]Django views: how to modify returned queryset before rendering in template?
[英]Django modify queryset values before rendering on the template
我有一个将数据发送到ListView
的FormView
。
在LisView
我使用self.request.GET
获取数据并取消 qustom 过滤器,我得到了我需要的查询集。 现在我需要修改查询集中的一些值,但我不知道如何修改。
尝试使用queriset['name']
或queryset[1]
对ListView
的查询集进行索引,但它告诉我该索引不受支持。
尝试应用queryset.values_list()
和queriset.values()
然后索引,但相同的结果出现。
试图在ListView
创建一个函数并应用到模板中,得到“无法解析提醒”。
最后尝试通过执行 object.value - request.GET.value 来保留模板中的值,但我收到此错误:
Could not parse the remainder: ' - request.GET.salary' from 'item.smg70 - request.GET.salary'
视图.py
class QuoteListView(ListView):
model = SmgQuotesTable
def get_queryset(self):
r_get = self.request.GET
d_get = {'name': None , 'email':None , 'couple': None, 'age': None, 'kids':None , 'salary':None}
for value in d_get:
d_get[value] = r_get[value]
query_filter = Quotes().planSelector(d_get['couple'], d_get['kids'], d_get['age'])
queryset = super(QuoteListView, self).get_queryset().filter(composite=query_filter)
for i in queryset[1:]:
i - d_get['salary']
print(i)
return queryset
def salary(self, x):
salary_get = self.request.GET('salary')
return x - salary_get
smgquotestable_list.html
{% for item in object_list %}
<div class="table-responsive text-nowrap"></div>
<table class="table table-striped">
<thead>
<tr>
<th scope="col"></th>
<th scope="col">SMG01</th>
<th scope="col">SMG02</th>
<th scope="col">SMG10</th>
<th scope="col">SMG20</th>
<th scope="col">SMG30</th>
<th scope="col">SMG40</th>
<th scope="col">SMG50</th>
<th scope="col">SMG60</th>
<th scope="col">SMG70</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">{{ item.composite }}</th>
<td>$ {{ item.smg01 }}</td>
<td>$ {{ item.smg02 }}</td>
<td>$ {{ item.smg10 }}</td>
<td>$ {{ item.smg20 }}</td>
<td>$ {{ item.smg30 }}</td>
<td>$ {{ item.smg40 }}</td>
<td>$ {{ item.smg50 }}</td>
<td>$ {{ item.smg60 }}</td>
<td>$ {{ item.smg70 }}</td>
</tr>
</tbody>
</table>
</div>
{% endfor %}
我需要将表单发送的薪水值保留为我在查询集中获得的 smg01 等值。
您还可以在查询集上使用更新或按照提到的 dirkgroten 进行迭代
您没有共享一些内容,例如 SmgQuotesTable 的模型字段或 Quote 和 planSelector 的代码。 但是,假设您尝试使用在 self.request 对象中获得的值(例如姓名、电子邮件、夫妇等的值)过滤结果,并假设这些实际上是 SmgQuotesTable 的字段,您应该以常规方式进行此操作(请参阅文档)。 另外,请注意,您不需要在 get_queryset 中调用 Super。 您可能会混淆 get_context_data
您需要调用 super 来获取上下文。
class QuoteListView(ListView):
model = SmgQuotesTable
def get_queryset(self):
r_get = self.request.GET
d_get = {'name': None , 'email':None , 'couple': None, 'age': None, 'kids':None , 'salary':None}
for value in d_get:
d_get[value] = r_get[value] # I do NOT know what you are doing here, so I am going by your code
filtered_quotes = SmgQuotesTable.objects.filter(**d_get)
return filtered_quotes
我的错误是不理解 dirkgroten 告诉我的,它是对象的一个实例。 所以我需要使用i.value
来使用访问值。
这是我解决它的方法:
在quotes.py
我编写了一个名为salaryDiscount
的函数,它在运行时修改值:
def salaryDiscount(self, queryset, salary):
'''
This function apply the salary discount to all the values for this person.
'''
salary = float(salary) * 0.03 # Take the salary, and aply the part that could be discounted
for i in queryset: # Get the object of the queryset
i.smg01 = round(i.smg01 - salary, 2) # Use the object edit the values on the fly
i.smg02 = round(i.smg02 - salary, 2)
i.smg10 = round(i.smg10 - salary, 2)
i.smg20 = round(i.smg20 - salary, 2)
i.smg30 = round(i.smg30 - salary, 2)
i.smg40 = round(i.smg40 - salary, 2)
i.smg50 = round(i.smg50 - salary, 2)
i.smg60 = round(i.smg60 - salary, 2)
i.smg70 = round(i.smg70 - salary, 2)
return queryset # Return the queryset edited.
然后我在修改get_queryset
集的ListView
的get_queryset
上调用它:
class QuoteListView(ListView):
model = SmgQuotesTable
def get_queryset(self):
r_get = self.request.GET # Get the data sendir by QuoteFormView
query_filter = Quotes().planSelector(r_get['couple'], r_get['kids'], r_get['age']) # Use the filter to get the correct Quote
queryset = super(QuoteListView, self).get_queryset().filter(composite=query_filter) # Aply the filter to get the correct queryset
Quotes().salaryDiscount(queryset,r_get['salary']) # Aply the discount of the salary
return queryset
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.