python - AttributeError:“QuerySet”对象没有属性“requestedDate”
问题描述
我只想将 CustomerPurchaseOrder(inputdate) 过滤到 CustomerPurchaseOrderDetail(requestedDate)。我怎么做?
这是我当前的views.py
record = CustomerPurchaseOrder.objects.filter(profile__in=client.values_list('id'))
date = CustomerPurchaseOrder.objects.filter(profile__in=client.values_list('id'))
status = CustomerPurchaseOrderDetail.objects.filter(profile__in=client.values_list('id')) \
.filter(customer_Purchase_Order__in=record.values_list('id')).filter(inputdate=date.requestedDate)
这是我的模型
class CustomerPurchaseOrder(models.Model):
profile = models.ForeignKey(Customer,
on_delete=models.SET_NULL, null=True, blank=True,
verbose_name="Client Account")
customerdeliveryaddress = models.ForeignKey(CustomerDeliveryAddress,
on_delete=models.SET_NULL, null=True, blank=True,
verbose_name="Delivery Address")
process = models.ForeignKey('Process',
on_delete=models.SET_NULL, null=True, blank=True,
verbose_name="Process")
requestedDate = models.DateField(auto_now_add=True)
class CustomerPurchaseOrderDetail(models.Model):
profile = models.ForeignKey(Customer,
on_delete=models.SET_NULL, null=True, blank=True,
verbose_name="Client Account")
products = models.ForeignKey(Product,
on_delete=models.SET_NULL, null=True, blank=True,
verbose_name="Product")
customer_Purchase_Order = models.ForeignKey(CustomerPurchaseOrder,
on_delete=models.SET_NULL, null=True, blank=True,
verbose_name="CustomerPurchaseOrder")
inputdate = models.DateField(auto_now_add=True)
这是我的完整追溯
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
116. .filter(customer_Purchase_Order__in=record.values_list('id')).filter(inputdate=records.requestedDate)
Exception Type: AttributeError at /client/
异常值:“QuerySet”对象没有属性“requestedDate”
更新
当我尝试这个
status = CustomerPurchaseOrderDetail.objects.filter(profile__in=client.values_list('id')) \
.filter(customer_Purchase_Order__in=record.values_list('id')).filter(inputdate=date.values('requestedDate'))
我收到此错误
The QuerySet value for an exact lookup must be limited to one result using slicing.
解决方案
您可以使用表达式尝试这样的操作,F()
它允许您从同一个对象访问模型字段:
status = CustomerPurchaseOrderDetail.objects.filter(profile__in=client.values_list('id')) \
.filter(customer_Purchase_Order__in=record.values_list('id')).filter(inputdate=F('customer_Purchase_Order__requestedDate'))
推荐阅读
- java - 为什么我收到 java.sql.SQLException:当 url 正确且驱动程序存在时找不到合适的驱动程序?
- python - 如何根据第一次出现的唯一列值获取行
- localforage - 如何为移动应用初始化 localForage
- javascript - 如何从异步函数中获取数据并在获取数据后允许下一个代码执行
- java - 限制在 firebase 实时数据库中添加的子项数量
- java - 如何在android studio中的背景图像上显示gif
- angular - 以 Angular 形式提交
- javascript - 与多个 index.html 反应
- java - 装饰器模式可以用行为模式代替来解决问题吗?
- mongodb - 无法在项目上执行目标 io.swagger:swagger-codegen-maven-plugin:2.2.3:generate (default) 代码生成失败