Python Django,如何只获取保存在数据库中的最新数据?
我有两种型号,PurchaseOrder和PurchaseOrderdetail,我只想知道如何通过过滤PurchaseOrderdetail来获取PurchaseOrder中保存的最新记录 这是我的模特Python Django,如何只获取保存在数据库中的最新数据?,python,django,Python,Django,我有两种型号,PurchaseOrder和PurchaseOrderdetail,我只想知道如何通过过滤PurchaseOrderdetail来获取PurchaseOrder中保存的最新记录 这是我的模特 class CustomerPurchaseOrder(models.Model): .... inputdate = models.DateTimeField(auto_now_add=True, verbose_name="OrderSubmittedDa
class CustomerPurchaseOrder(models.Model):
....
inputdate = models.DateTimeField(auto_now_add=True, verbose_name="OrderSubmittedDateTime")
....
class CustomerPurchaseOrderDetail(models.Model):
....
customer_Purchase_Order = models.ForeignKey(CustomerPurchaseOrder,
on_delete=models.SET_NULL, null=True, blank=True,
verbose_name="CustomerPurchaseOrder")
这是我的观点
allrelatedProduct = CustomerPurchaseOrderDetail.objects.filter(profile__in=client.values_list('id')).latest('customer_Purchase_Order__inputdate')
这就是我得到的错误
'CustomerPurchaseOrderDetail' object is not iterable
我的全部回溯
Template error:
In template C:\Users\USER\Desktop\LastProject\OnlinePalengke\customAdmin\templates\customAdmin\clientAdmin.html, error at line 341
'CustomerPurchaseOrderDetail' object is not iterable
331 : <h1>My Account</h1>
332 :
333 : <div class="grid">
334 : <div class="grid__item medium-up--two-thirds">
335 :
336 : <div class="content-block">
337 :
338 : <h2>Order History </h2>
339 :
340 : <div class="scrollable-container">
341 : {% for history_product in allrelatedProduct %}
342 :
343 : <table class="responsive-table cart-table">
344 : <thead class="cart__row visually-hidden">
345 : <th colspan="2">Product</th>
346 : <th>Quantity</th>
347 : <th>Total</th>
348 : </thead>
349 : <tbody id="CartProducts">
350 : <tr class="cart__row responsive-table__row">
351 : <td class="cart__cell--image text-center">
Traceback:
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py" in _get_response
115. response = self.process_exception_by_middleware(e, request)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py" in _get_response
113. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\USER\Desktop\LastProject\OnlinePalengke\customAdmin\decorators.py" in wrapper_func
42. return view_func(request, *args, **kwargs)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
21. return view_func(request, *args, **kwargs)
File "C:\Users\USER\Desktop\LastProject\OnlinePalengke\customAdmin\views.py" in client
124. "allrelatedProduct": allrelatedProduct})
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\shortcuts.py" in render
36. content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\loader.py" in render_to_string
62. return template.render(context, request)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\backends\django.py" in render
61. return self.template.render(context)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render
171. return self._render(context)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in _render
163. return self.nodelist.render(context)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render
937. bit = node.render_annotated(context)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render_annotated
904. return self.render(context)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\defaulttags.py" in render
165. values = list(values)
Exception Type: TypeError at /client/
Exception Value: 'CustomerPurchaseOrderDetail' object is not iterable
模板错误:
在模板C:\Users\USER\Desktop\LastProject\OnlinePalengke\customAdmin\templates\customAdmin\clientAdmin.html中,第341行出现错误
“CustomerPurchaseOrderDetail”对象不可编辑
331:我的账户
332 :
333 :
334 :
335 :
336 :
337 :
338:订单历史记录
339 :
340 :
341:{allrelatedProduct%}中的历史记录产品的%
342 :
343 :
344 :
345:产品
346:数量
347:总计
348 :
349 :
350 :
351 :
回溯:
文件“C:\Users\USER\AppData\Local\Programs\Python37-32\lib\site packages\django\core\handlers\exception.py”位于内部
34响应=获取响应(请求)
获取响应中的文件“C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site packages\django\core\handlers\base.py”
115response=self.process\u异常\u由\u中间件(e,请求)
获取响应中的文件“C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site packages\django\core\handlers\base.py”
113响应=包装的回调(请求,*回调参数,**回调参数)
包装函数中的文件“C:\Users\USER\Desktop\LastProject\OnlinePalengke\customAdmin\decorators.py”
42返回视图功能(请求,*args,**kwargs)
视图中的文件“C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site packages\django\contrib\auth\decorators.py”
21返回视图功能(请求,*args,**kwargs)
客户端中的文件“C:\Users\USER\Desktop\LastProject\OnlinePalengke\customAdmin\views.py”
124“allrelatedProduct”:allrelatedProduct})
render中的文件“C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site packages\django\shortcuts.py”
36content=loader.render_to_string(模板名称、上下文、请求、using=using)
文件“C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site packages\django\template\loader.py”,位于render\u to\u字符串中
62返回template.render(上下文、请求)
render中的文件“C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site packages\django\template\backends\django.py”
61返回self.template.render(上下文)
render中的文件“C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site packages\django\template\base.py”
171返回self.\u呈现(上下文)
文件“C:\Users\USER\AppData\Local\Programs\Python37-32\lib\site packages\django\template\base.py”位于\u render中
163返回self.nodelist.render(上下文)
render中的文件“C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site packages\django\template\base.py”
937位=节点。带注释的渲染(上下文)
render\u注释中的文件“C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site packages\django\template\base.py”
904返回self.render(上下文)
render中的文件“C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site packages\django\template\defaulttags.py”
165值=列表(值)
异常类型:位于/客户端的TypeError/
异常值:“CustomerPurchaseOrderDetail”对象不可编辑
要获取与最新客户采购订单相关的所有客户采购订单详细信息
对象,请执行以下操作:
last_order = CustomerPurchaseOrder.objects.order_by('inputdate').last()
# or .order_by('-inputdate').first()
# you can add more filters like: user=user_obj
allrelatedProduct = CustomerPurchaseOrderDetail.objects.filter(customer_Purchase_Order=last_order)
现在,如果要渲染所有结果:
{% for history_product in allrelatedProduct %}
<p>history_product.inputdate</p>
{% endfor %}
{%用于allrelatedProduct%}
历史\产品输入日期
{%endfor%}
但如果你只想要一个:
<p>{{allrelatedProduct.first.inputdate}}</p>
{{allrelatedProduct.first.inputdate}
您还可以在视图中获取所需的一个对象,并将其传递给模板,然后删除。首先
,这样就可以使用allrelatedProduct.inputdate
调用它。您使用的是latest()
方法,该方法返回查询集中的最后一个对象。您使用的for循环不能使用,因为allrelatedProduct
将只是一个对象,您不能在对象上迭代。那么如何在html中打印它?回答时提供了完整的详细信息。