gpt4 book ai didi

python-3.x - 使用 Django 序列化程序类保存嵌套数据

转载 作者:行者123 更新时间:2023-12-05 05:14:59 26 4
gpt4 key购买 nike

我有一个 json 数据要保存到 django 模型中。当我尝试保存它时,我遇到了各种各样的错误,我试图解决它,但都无济于事。请帮助我,我是 django rest 框架的新手。这是我要插入的模型:

class OrderItemDishItem(SoftDeleteModelBase):
item = models.ForeignKey(DishItem, on_delete=models.CASCADE)
quantity = models.PositiveIntegerField(default=1)
price = models.DecimalField(max_digits=5, decimal_places=2,default=0.00)


class OrderItem(SoftDeleteModelBase):
dish = models.ForeignKey(Dish, on_delete=models.CASCADE)
items = models.ManyToManyField(OrderItemDishItem, related_name="dish_items")
quantity = models.PositiveIntegerField(default=1)
unit_price = models.DecimalField(max_digits=5, decimal_places=2,default=0.00)
discount_amount = models.DecimalField(max_digits=5, decimal_places=2,default=0.00)
discount_percent = models.DecimalField(max_digits=5, decimal_places=2,default=0.00)


class Order(SoftDeleteModelBase):
foodjoint=models.ForeignKey(FoodJoint, on_delete=models.CASCADE, related_name="orders", null=True)
customer=models.ForeignKey(CustomerProfile,on_delete=models.CASCADE,null=True)
items = models.ManyToManyField(to=OrderItem, related_name="order")
total_cost = models.DecimalField(max_digits=5, decimal_places=2,default=0.00)
discount_amount = models.DecimalField(max_digits=5, decimal_places=2,default=0.00)
discount_percent = models.DecimalField(max_digits=5, decimal_places=2,default=0.00)

这是我的每个序列化器类:

class OrderItemDishItemSerializer(serializers.ModelSerializer):
def __init__(self, *args, **kwargs):
many = kwargs.pop('many', True)
super(OrderItemDishItemSerializer, self).__init__(many=many, *args, **kwargs)

class Meta:
model = models.OrderItemDishItem
fields = '__all__'
depth = 1

def create(self, validated_data):
dish_item = validated_data.pop("items")


item_obj = models.OrderItemDishItem.objects.create(**item)

validated_data["items"]=item_obj
return super().create(validated_data)


class OrderItemSerializer(serializers.ModelSerializer):
items = OrderItemDishItemSerializer(many=True)

def __init__(self, *args, **kwargs):
many = kwargs.pop('many', True)
super(OrderItemSerializer, self).__init__(many=many, *args, **kwargs)

class Meta:
model = models.OrderItem
fields = '__all__'

def create(self, validated_data):
items = validated_data.pop("items")
for order in items:
item = OrderItemDishItemSerializer(data=order["items"])
models.OrderItem.objects.create(
item_id= item.initial_data,
quantity= order["quantity"],
unit_price=order['unit_price']
)
validated_data["items"]=item.initial_data
return super().create(validated_data)

class OrderSerializer(serializers.ModelSerializer):
customer = CustomerSerializer()
items = OrderItemSerializer(many=True)
class Meta:
model = models.Order
fields = '__all__'
depth = 1
def create(self, validated_data):
user_account= validated_data["customer"].pop("user_account")
# user_account['password'] = make_password(user_account['password'])
user = User.objects.create_user(**user_account)

customer = validated_data.pop("customer")

customer = models.CustomerProfile.objects.create(
mobile_number= customer["mobile_number"],
address= customer["address"],
user_account= user
)

# add OrderItems
order_items = validated_data.pop("items")
items = OrderItemSerializer(data=order_items)

validated_data["items"] = items.initial_data
validated_data["customer"] = customer
# validated_data["customer"]["user_account"] = user
return super().create(validated_data)

这是我发布的 json 数据:

{
"foodjoint_id": 1,
"customer": {
"mobile_number": 2013717123,
"address": "Sunyani",
"picture": null,
"user_account": {
"username": 2013717123,
"first_name": "Rahman",
"last_name": "Lawal",
"email": "email@email.com",
"password": "test1234"
}
},
"items": [
{
"items": [
{
"quantity": 1,
"price": 22,
"item": 2
},
{
"quantity": 1,
"price": 10,
"item": 4
},
{
"quantity": 1,
"price": 2,
"item": 6
},
{
"quantity": 1,
"price": 0,
"item": 12
},
{
"quantity": 1,
"price": 0,
"item": 13
},
{
"quantity": 1,
"price": 5,
"item": 9
}
],
"unit_price": 22,
"quantity": 1,
"dish": 2
}
],
"total_cost": 0,
"discount_amount": 0,
"discount_percent": 0

}

请问如何保存数据?

我得到的错误:

When a serializer is passed a `data` keyword argument you must call `.is_valid()` before attempting to access the serialized `.data` representation.
You should either call `.is_valid()` first, or access `.initial_data` instead.

最佳答案

在键入您的序列化数据之前,尝试调用对您传入的数据有效。

items = OrderItemSerializer(data=order_items)
if items.is_valid():
validated_data["items"] = items.initial_data
validated_data["customer"] = customer

关于python-3.x - 使用 Django 序列化程序类保存嵌套数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52004935/

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