django jsonfield字段更新的坑

 

 

简单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"
    }
  ]
}

 

    m_attrs = obj.module_attrs
    if bus_obj:
        for env in ['test', 'pre', 'prod']:
            # m_attrs[env]["kube_cluster"] = bus_obj.default_cluster
            if env == "test":
                m_attrs[env]["kube_cluster"] = bus_obj.test_cluster
            if env == "pre":
                m_attrs[env]["namespace"] = bus_obj.name_space
                m_attrs[env]["kube_cluster"] = bus_obj.pre_cluster
                if bus_obj.business_name in zhenai_biz:
                    m_attrs[env]["profile"] = "qa"
                if bus_obj.business_name in crm_biz:
                    m_attrs[env]["profile"] = "pre"
            if env == "prod":
                m_attrs[env]["namespace"] = bus_obj.name_space
                m_attrs[env]["kube_cluster"] = bus_obj.prod_cluster
                if bus_obj.business_name in z_biz:
                    m_attrs[env]["profile"] = "online"
                if bus_obj.business_name in crm_biz:
                    m_attrs[env]["profile"] = "prod"
            # if self.module_attrs[env]["kube_cluster"] == 'offline':
            #     self.module_attrs[env]["logs"] = '/data/logs'
        obj.module_attrs = m_attrs
        obj.save()

  

 

posted @ 2023-03-24 12:08  whitesky-root  阅读(185)  评论(0编辑  收藏  举报