代码之家  ›  专栏  ›  技术社区  ›  Doing Things Occasionally

使用Queryset将具有特定外键的用户与键匹配时发生元组错误

  •  0
  • Doing Things Occasionally  · 技术社区  · 4 年前

    如何在JsonResponse中格式化并返回查询中的所有项?我不明白这个错误。

    这是查询的结果

    TypeError at /api/driver/orders/ready/
    'tuple' object is not callable
    
     /apis.py in driver_get_ready_orders
    @csrf_exempt
    def driver_get_ready_orders(request):
        driver = CustomJWTAuthenticator().authenticate(request)[0].driver
        queryset = Order.objects.filter(
            status=Order.READY, driver=None).order_by("-id"),
        if driver.dispensary:
            queryset = queryset(dispensary=driver.dispensary ...
                                )
        orders = Order.objects.filter(
            queryset,
            many=True,
            context={"request": request}
    ▶ Local vars
    Variable    Value
    driver  
    <Driver: Malcom Brooks>
    queryset    
    (<QuerySet [<Order: 10>, <Order: 8>]>,)
    request 
    <WSGIRequest: GET '/api/driver/orders/ready/?
    

       def driver_get_ready_orders(request):
    
    
        driver = CustomJWTAuthenticator().authenticate(request)[0].driver
        queryset = Order.objects.filter(
            status=Order.READY, driver=None).order_by("-id"),
        if driver.dispensary:
            queryset = queryset(dispensary=driver.dispensary
                                            ).all()
        orders = Order.objects.filter(
                queryset,
                many=True
    
    
        )
    
        return JsonResponse({"orders": orders.data})
    

    订单和驱动程序型号:

    class Order(models.Model):
        PICKING = 1
        READY = 2
        ONTHEWAY = 3
        DELIVERED = 4
    
        CANCELED = 5
    
        STATUS_CHOICES = (
            (PICKING, "Your Order Is Being Picked Right Off The Plant!"),
            (READY, "Picked, Weighed, And Ready For Delivery."),
            (ONTHEWAY, "The Best Bud Is Heading Straight To Your Door."),
            (DELIVERED, "Delivered"),
    
            (CANCELED, "Canceled/Refunded")
        )
        address = models.CharField(max_length=500)
    
        customer = models.ForeignKey(CustomerDetails, on_delete=models.CASCADE)
    
        dispensary = models.ForeignKey(Dispensary, on_delete=models.CASCADE)
        driver = models.ForeignKey(
            Driver, blank=True, null=True, on_delete=models.CASCADE)
    
    status = models.IntegerField(choices=STATUS_CHOICES)
    
    def __str__(self):
    
        return str(self.id)
    
    class Driver(models.Model):
        Active_License = (
            ('License_Status', (
                ('active', 'Active'),
                ('suspended', 'Suspended')
            )),
        )
        address = models.CharField(max_length=500, blank=True)
    
    
        name = models.CharField(max_length=500, default='')
        phone = models.CharField(max_length=500, blank=True)
        photo = models.ImageField(
            upload_to='driver_photos/', blank=False, default='')
    
        user = models.OneToOneField(
            User, on_delete=models.CASCADE, related_name='driver')
        dispensary = models.ForeignKey(
            Dispensary, null=True, blank=True, on_delete=models.CASCADE)
    
    
        def __str__(self):
            return self.user.get_full_name()
    
    1 回复  |  直到 4 年前
        1
  •  2
  •   willeM_ Van Onsem    4 年前

    这里有三个问题:

    1. queryset 可赎回,因此 queryset = queryset(dispensary=driver.dispensary)
    2. 你也不能用 .filter(queryset) . 你可能想用 :

    因此,您应将其改写为:

    # no trailing comma
    queryset = Order.objects.filter(status=Order.READY, driver=None).order_by('-id')
    if driver.dispensary:
        queryset = queryset.filter(dispensary=driver.dispensary)
    orders = OrderSerializer(queryset, many=True)