DRF3 M2M嵌套序列化器删除嵌套的“id”#39;更新字段()

时间:2015-03-17 20:37:41

标签: python django django-rest-framework

我正在尝试创建一个功能齐全的M2M(通过表格)嵌套序列化程序,它在create()上运行良好。但是,当我接受包含嵌套记录的id的串行化程序的GET版本返回的JSON并对同一个序列化程序执行PUT时,'id'字段将从嵌套记录validated_data中移除到它到达update()方法。

{
"id": 1, 
"addresses": [
    {
        "id": 1,  # This is ripped out
        "city": "Oakville", 
        "addr": "13 Main St", 
        "postal_code": "01101"
    }, 
    {
        "id": 2,  # This is ripped out
        "city": "Watertown", 
        "addr": "88 Main St", 
        "postal_code": "01101"
    }, 
    "customer_number": 1234, 
    "customer_type": 1, 
    "pricing_sequence": 2, 
    "name": "Customer number 1234"
 }

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

是的,这是django-rest-framework: serializer from DATA don't update model ID

的副本

我想我明白了。默认情况下,ModelSerializer似乎会生成' id'字段只读。解决方案是添加一个明确的ID'字段到序列化器。请参阅tomchristie的评论https://github.com/tomchristie/django-rest-framework/issues/2114