首页 > 解决方案 > 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.

标签: pythondjango

解决方案


您可以使用表达式尝试这样的操作,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'))

推荐阅读