老师, 课程里面创建订单的时候是将购物车所有的商品全部加入订单中, 但是现在淘宝上市根据选取的商品来创建订单, 在serializer中该使用啥字段保存商品和商品数量呀,
class OrderInfoSerializer(serializers.Serializer):
goods = serializers.IntegerField(required=True, label="商品", error_messages={
"required": "商品不能为空"
})
goods_num = serializers.IntegerField(required=True, label="商品数量", min_value=1, error_messages={
"required": "商品数量不能为空",
"min_value": "商品数量不能小于1"
})
class GoodsOrderSerializer(serializers.Serializer):
user = serializers.HiddenField(
default=serializers.CurrentUserDefault()
)
order_no = serializers.CharField(read_only=True)
status = serializers.IntegerField(read_only=True)
pay_time = serializers.DateTimeField(read_only=True)
trade_no = serializers.CharField(read_only=True)
order_message = serializers.CharField(label="订单留言", required=False)
order_amount = serializers.DecimalField(max_digits=20, decimal_places=2, required=True,
error_messages={
"required": "订单金额不能为空"
})
address = serializers.CharField(required=True, error_messages={
"required": "请填写收货地址"
})
signer_name = serializers.CharField(label="收件人", required=False)
signer_mobile = serializers.CharField(required=True, max_length=11, error_messages={
"required": "请填写收件人手机号码",
"max_length": "手机号码有误",
})
goods = OrderInfoSerializer(many=True)
def generate_order_sn(self):
# 当前时间+userid+随机数
from random import Random
random_ins = Random()
order_sn = "{time_str}{userid}{ranstr}".format(time_str=time.strftime("%Y%m%d%H%M%S"),
userid=self.context["request"].user.id,
ranstr=random_ins.randint(10, 99))
return order_sn
def create(self, validated_data):
order_no = self.generate_order_sn()
validated_data["order_no"] = order_no
goods = validated_data["goods"]
del validated_data["goods"]
goods_order = GoodsOrder.objects.create(**validated_data)
for good in goods:
order_info = OrderInfo()
goods = Goods.objects.filter(id=good["goods"]).first()
order_info.goods = goods
order_info.goods_num = good["goods_num"]
order_info.order_id = goods_order.id
order_info.save()
return goods_order
报错:
Internal Server Error: /trade/order/
Traceback (most recent call last):
File "D:\vienv\env_django\lib\site-packages\rest_framework\fields.py", line 454, in get_attribute
return get_attribute(instance, self.source_attrs)
File "D:\vienv\env_django\lib\site-packages\rest_framework\fields.py", line 94, in get_attribute
instance = getattr(instance, attr)
AttributeError: 'GoodsOrder' object has no attribute 'goods'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\vienv\env_django\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
response = get_response(request)
File "D:\vienv\env_django\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "D:\vienv\env_django\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\vienv\env_django\lib\site-packages\django\views\decorators\csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "D:\vienv\env_django\lib\site-packages\rest_framework\viewsets.py", line 114, in view
return self.dispatch(request, *args, **kwargs)
File "D:\vienv\env_django\lib\site-packages\rest_framework\views.py", line 505, in dispatch
response = self.handle_exception(exc)
File "D:\vienv\env_django\lib\site-packages\rest_framework\views.py", line 465, in handle_exception
self.raise_uncaught_exception(exc)
File "D:\vienv\env_django\lib\site-packages\rest_framework\views.py", line 476, in raise_uncaught_exception
raise exc
File "D:\vienv\env_django\lib\site-packages\rest_framework\views.py", line 502, in dispatch
response = handler(request, *args, **kwargs)
File "D:\vienv\env_django\lib\site-packages\rest_framework\mixins.py", line 20, in create
headers = self.get_success_headers(serializer.data)
File "D:\vienv\env_django\lib\site-packages\rest_framework\serializers.py", line 562, in data
ret = super().data
File "D:\vienv\env_django\lib\site-packages\rest_framework\serializers.py", line 260, in data
self._data = self.to_representation(self.instance)
File "D:\vienv\env_django\lib\site-packages\rest_framework\serializers.py", line 516, in to_representation
attribute = field.get_attribute(instance)
File "D:\vienv\env_django\lib\site-packages\rest_framework\fields.py", line 487, in get_attribute
raise type(exc)(msg)
AttributeError: Got AttributeError when attempting to get a value for field `goods` on serializer `GoodsOrderSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `GoodsOrder` instance.
Original exception text was: 'GoodsOrder' object has no attribute 'goods'.
Django REST framework课程视频,RESTFul API前后端分离开发
了解课程