django jsonfield字段更新的坑

发布时间 2023-03-24 12:14:46作者: whitesky-root

 

 

简单json字段:{"name":"test", "age":30, "email":"tet@gmail.com"},  可以通过obj.jsonfiledName["name"] ="test2",  obj.save()方式可成功写入数据库;

复杂json字段如,更新number字段,需使用特殊的更新方式:https://django-postgres-extensions.readthedocs.io/en/latest/json.html; 或者单独处理obj.jsonfiledName = after_update_jsonfiledName, obj.save()

 

{
  "name": "John Smith",
  "age": 30,
  "email": "john.smith@example.com",
  "address": {
    "street": "123 Main St",
    "city": "Anytown",
    "state": "CA",
    "zip": "12345"
  },
  "phoneNumbers": [
    {
      "type": "home",
      "number": "555-555-1234"
    },
    {
      "type": "work",
      "number": "555-555-5678"
    }
  ]
}