To save this JSON data in a Django model field of type JSONField in MySQL, follow these steps:

  1. Import the JSONField from django.db.models.fields.json:
from django.db import models
from django.db.models.fields.json import JSONField
  1. Define your model with a JSONField:
class ApprovalRequest(models.Model):
    request_data = JSONField()
  1. Create a new instance of your model and set the request_data field to your JSON data:
import json

data = {
    "allowAction": [
        "Approve"
    ],
    "assignmentType": "Single User",
    "userEntity": [
        {
            "id": "mailto:alok@getmaxlisteners.com",
            "value": "Alok"
        }
    ],
    "timeLimit": "5",
    "timeLimitType": "Hour",
    "completionPolicy": "Majority of approvers"
}

approval_request = ApprovalRequest.objects.create(request_data=json.dumps(data))
  1. To retrieve the JSON data from the database, simply access the request_data field:
approval_request = ApprovalRequest.objects.get(id=1)
data = json.loads(approval_request.request_data)
allowAction Approve assignmentType Single User userEntity id mailtoalokgetmaxlistenerscom value Alok timeLimit 5 timeLimitType Hour

原文地址: https://www.cveoy.top/t/topic/6wm 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录