首页 > 其他分享 >drf反序列化时出错

drf反序列化时出错

时间:2023-02-21 21:56:59浏览次数:36  
标签:序列化 name quantity items amount 出错 total drf

- 问题

  • Postman传递json,drf反序列化时接受不到值,一直提示None

- json数据

点击查看代码
{
    "number": "1",
    "warehouse": "1",
    "members": "1",
    "handler": "1",
    "handle_time": "2023-01-28",
    "remark": "123",
    "total_quantity": "10",
    "total_amount": "100",
    "discount_amount": "0",
    "creator": 1,
    "pos_goods_items": [
        {
            "goods": 2,
            "sales_quantity": 30,
            "sales_price": 2,
            "total_amount": 60,
            "return_quantity": 0
        }
    ],
    "pay_record_items": []
}

- 序列化字段

点击查看代码
read_only_fields = ['id', 'handle_time', 'create_time', 'members_name', 'handler_name', 'creator_name']
        fields = ['members', 'warehouse', 'remark', 'total_quantity', 'discount', 'total_amount', 'creator', 'handler',
                  'number', 'discount_amount', 'is_void', 'enable_auto_stock_out',
                  'pos_goods_items', 'pay_record_items', *read_only_fields]

- 报错信息

sqlite NOT NULL constraint failed

- 解决办法

  • 将model.py中关联的外键,做引入填写
    warehouse = ForeignKey(Warehouse, on_delete=PROTECT, related_name='pos_order', verbose_name='仓库')

标签:序列化,name,quantity,items,amount,出错,total,drf
From: https://www.cnblogs.com/bao-pu/p/17142577.html

相关文章