TypeError: cannot unpack non-iterable int object in Django rest framework
TypeError: cannot unpack non-iterable int object in Django rest framework
我正在尝试获取与该商家关联的那些产品的订单列表。在我的项目中,每个产品都与商家相关联。在商家仪表板中,商家应该只能查看他们的产品和订单。
当我尝试根据与该商家关联的产品过滤订单时,出现上述错误。
我的模特:
class Product(models.Model):
merchant = models.ForeignKey(Seller,on_delete=models.CASCADE,blank=True,null=True)
category = models.ManyToManyField(Category, blank=False)
brand = models.ForeignKey(Brand, on_delete=models.CASCADE)
class Order(models.Model):
ORDER_STATUS = (
('To_Ship', 'To Ship',),
('Shipped', 'Shipped',),
('Delivered', 'Delivered',),
('Cancelled', 'Cancelled',),
)
user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True)
#items = models.ManyToManyField(OrderItem,blank=True, null=True,related_name="order_items")
#start_date = models.DateTimeField(auto_now_add=True)
order_status = models.CharField(max_length=50,choices=ORDER_STATUS,default='To_Ship')
ordered_date = models.DateTimeField(auto_now_add=True)
ordered = models.BooleanField(default=False)
total_price = models.CharField(max_length=50,blank=True,null=True)
#billing_details = models.OneToOneField('BillingDetails',on_delete=models.CASCADE,null=True,blank=True,related_name="order")
def __str__(self):
return self.user.email
class Meta:
verbose_name_plural = "Orders"
ordering = ('-id',)
class OrderItem(models.Model):
#user = models.ForeignKey(User,on_delete=models.CASCADE, blank=True)
order = models.ForeignKey(Order,on_delete=models.CASCADE, blank=True,null=True,related_name='order_items')
item = models.ForeignKey(Product, on_delete=models.CASCADE,blank=True, null=True)
order_variants = models.ForeignKey(Variants,on_delete=models.CASCADE,blank=True,null=True)
quantity = models.IntegerField(default=1)
total_item_price = models.CharField(max_length=50,blank=True,null=True)
def __str__(self):
return f"{self.quantity} items of {self.item} of {self.order.user}"
class Meta:
verbose_name_plural= "Cart Items"
ordering = ('-id',)
我的看法:
class SellerOrderAPIView(ListAPIView):
permission_classes = [AllowAny]
serializer_class = OrderItemSerializer
def get_queryset(self):
merchant = get_object_or_404(Seller,self.kwargs['pk'])
return OrderItem.objects.all(item__merchant=merchant)
我的序列化程序:
class OrderItemSerializer(serializers.ModelSerializer):
order_variants = VariantSerializer(read_only=True)
#order_variants =VariantSerializer()
item = ProductSerializer(read_only=True)
class Meta:
model = OrderItem
fields = ['id','order','item','order_variants', 'quantity']
# depth = 1
在这里,我看到当我看到 orderdetails 时,merhchant 在 item 里面,这就是为什么我尝试使用 item__merchant=merchant 过滤查询集,但是我得到了这个错误。
我的网址:
path('api/merchantorder/<int:pk>', views.SellerOrderAPIView.as_view(), name='api-merchant-orderdetail'),
您需要指定过滤字段的名称:
class SellerOrderAPIView(ListAPIView):
permission_classes = [AllowAny]
serializer_class = OrderItemSerializer
def get_queryset(self):
merchant = get_object_or_404(Seller<b>, pk=self.kwargs['pk']</b>)
return OrderItem.objects.filter(item__merchant=merchant)
但是您可以简单地避免查找并使用:
class SellerOrderAPIView(ListAPIView):
permission_classes = [AllowAny]
serializer_class = OrderItemSerializer
def get_queryset(self):
return OrderItem.objects.filter(<b>item__merchant=self.kwargs['pk']</b>)
我正在尝试获取与该商家关联的那些产品的订单列表。在我的项目中,每个产品都与商家相关联。在商家仪表板中,商家应该只能查看他们的产品和订单。
当我尝试根据与该商家关联的产品过滤订单时,出现上述错误。
我的模特:
class Product(models.Model):
merchant = models.ForeignKey(Seller,on_delete=models.CASCADE,blank=True,null=True)
category = models.ManyToManyField(Category, blank=False)
brand = models.ForeignKey(Brand, on_delete=models.CASCADE)
class Order(models.Model):
ORDER_STATUS = (
('To_Ship', 'To Ship',),
('Shipped', 'Shipped',),
('Delivered', 'Delivered',),
('Cancelled', 'Cancelled',),
)
user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True)
#items = models.ManyToManyField(OrderItem,blank=True, null=True,related_name="order_items")
#start_date = models.DateTimeField(auto_now_add=True)
order_status = models.CharField(max_length=50,choices=ORDER_STATUS,default='To_Ship')
ordered_date = models.DateTimeField(auto_now_add=True)
ordered = models.BooleanField(default=False)
total_price = models.CharField(max_length=50,blank=True,null=True)
#billing_details = models.OneToOneField('BillingDetails',on_delete=models.CASCADE,null=True,blank=True,related_name="order")
def __str__(self):
return self.user.email
class Meta:
verbose_name_plural = "Orders"
ordering = ('-id',)
class OrderItem(models.Model):
#user = models.ForeignKey(User,on_delete=models.CASCADE, blank=True)
order = models.ForeignKey(Order,on_delete=models.CASCADE, blank=True,null=True,related_name='order_items')
item = models.ForeignKey(Product, on_delete=models.CASCADE,blank=True, null=True)
order_variants = models.ForeignKey(Variants,on_delete=models.CASCADE,blank=True,null=True)
quantity = models.IntegerField(default=1)
total_item_price = models.CharField(max_length=50,blank=True,null=True)
def __str__(self):
return f"{self.quantity} items of {self.item} of {self.order.user}"
class Meta:
verbose_name_plural= "Cart Items"
ordering = ('-id',)
我的看法:
class SellerOrderAPIView(ListAPIView):
permission_classes = [AllowAny]
serializer_class = OrderItemSerializer
def get_queryset(self):
merchant = get_object_or_404(Seller,self.kwargs['pk'])
return OrderItem.objects.all(item__merchant=merchant)
我的序列化程序:
class OrderItemSerializer(serializers.ModelSerializer):
order_variants = VariantSerializer(read_only=True)
#order_variants =VariantSerializer()
item = ProductSerializer(read_only=True)
class Meta:
model = OrderItem
fields = ['id','order','item','order_variants', 'quantity']
# depth = 1
在这里,我看到当我看到 orderdetails 时,merhchant 在 item 里面,这就是为什么我尝试使用 item__merchant=merchant 过滤查询集,但是我得到了这个错误。
我的网址:
path('api/merchantorder/<int:pk>', views.SellerOrderAPIView.as_view(), name='api-merchant-orderdetail'),
您需要指定过滤字段的名称:
class SellerOrderAPIView(ListAPIView):
permission_classes = [AllowAny]
serializer_class = OrderItemSerializer
def get_queryset(self):
merchant = get_object_or_404(Seller<b>, pk=self.kwargs['pk']</b>)
return OrderItem.objects.filter(item__merchant=merchant)
但是您可以简单地避免查找并使用:
class SellerOrderAPIView(ListAPIView):
permission_classes = [AllowAny]
serializer_class = OrderItemSerializer
def get_queryset(self):
return OrderItem.objects.filter(<b>item__merchant=self.kwargs['pk']</b>)