gpt4 book ai didi

django - 'Product' 类型的对象不是 JSON 可序列化的

转载 作者:行者123 更新时间:2023-12-04 20:37:50 26 4
gpt4 key购买 nike

我正在尝试通过 Django 从我的应用程序中提取 。问题是当我通过序列化程序调用 Order Detail 时,出现此错误:

TypeError at /api/customer/order/latest/
Object of type 'Product' is not JSON serializable
Request Method: GET
Request URL: http://localhost:8000/api/customer/order/latest/?access_token=XXXXXXXXXXXXXXXXXXXXXXXXXXXX
Django Version: 1.10
Exception Type: TypeError
Exception Value:
Object of type 'Product' is not JSON serializable

我正在从这个模型中提取数据:
class OrderDetail(models.Model):
order = models.ForeignKey(Order, related_name='order_details')
product_size = models.ForeignKey(ProductSize)
quantity = models.IntegerField()
sub_total = models.FloatField()

def __str__(self):
return str(self.id)

# references Prodcut and allows old code to work.
@property
def product(self):
return self.product_size.product

这是正在拉动的内容:
'order_details': [OrderedDict([('id', 68),
('product_size', 44),
('quantity', 1),
('sub_total', 20.0),
('product', <Product: Bacon Burger - withDrink>)])],
'status': 'Your Order Is Being Picked Right Off The Plant!',
'total': 20.0}
request
<WSGIRequest: GET '/api/customer/order/latest/?access_token=XXXXXXXXXXXXXXXXXXXXXXXXXXXX'>

序列化器:
class OrderDetailSerializer(serializers.ModelSerializer):

class Meta:
model = OrderDetail
fields = ("id", "product_size", "quantity", "sub_total", "product")


class OrderSerializer(serializers.ModelSerializer):
customer = OrderCustomerSerializer()
driver = OrderDriverSerializer()
restaurant = OrderRestaurantSerializer()
order_details = OrderDetailSerializer(many = True)
status = serializers.ReadOnlyField(source= "get_status_display")

class Meta:
model = Order
fields = ("id", "customer", "restaurant", "driver", "order_details", "total", "status", "address")

详细功能:
def customer_get_latest_order(request):
access_token = AccessToken.objects.get(token = request.GET.get("access_token"),
expires__gt = timezone.now())

customer = access_token.user.customer
order = OrderSerializer(Order.objects.filter(customer = customer).last()).data

return JsonResponse({"order": order})

我不确定需要做什么。

最佳答案

product OrderDetail 的属性(property)模型返回 Product对象,在响应过程中不能序列化。

要修复它,您只需返回 product.id :

class OrderDetail(models.Model):
order = models.ForeignKey(Order, related_name='order_details')
product_size = models.ForeignKey(ProductSize)
quantity = models.IntegerField()
sub_total = models.FloatField()

def __str__(self):
return str(self.id)

@property
def product(self):
return self.product_size.product.id

或者,如果您需要产品的详细信息作为响应,您应该在 OrderDetailSerializer 中再添加一个嵌套的序列化程序。 :
class ProductSerializer(serializers.ModelSerializer):

class Meta:
model = Product
fields = ("id", "other fields")

class OrderDetailSerializer(serializers.ModelSerializer):
prodcut = ProductSerializer()

class Meta:
model = OrderDetail
fields = ("id", "product_size", "quantity", "sub_total", "product")

关于django - 'Product' 类型的对象不是 JSON 可序列化的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50226159/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com