在 DJango 中将多值 json 格式字典存储到 mysql table 中的问题

Issue storing multi valued json format dictionary into mysql table in DJango

我的模板中有以下 ajax 调用

temp.html

function saveprof() {
            $.ajax({
                type: "POST",
                url: "saveprof",
                async: true,
                data: {
                    'radinput_Aj' : fun(),
                    'csrfmiddlewaretoken': $("input[name=csrfmiddlewaretoken]").val()
                },
                success: function (data, textStatus, jqXHR) {
                    $('#message').html(data);
                }
            });
        }

fun() returns 一个 json 格式对象 v - 如下所示:

示例:

var v = [
             { axis: "", value: 4, order: 0 },
             { axis: "", value: 4, order: 1 },
             { axis: "", value: 4, order: 2 },
             { axis: "", value: 4, order: 3 },
             { axis: "", value: 4, order: 4 },
             { axis: "", value: 4, order: 5 },
             { axis: "", value: 4, order: 6 }
    ];

Views.py

def saveprof(request):
if request.method == "POST":
    radinputV=[]
    radinputV = request.POST.get('radinput_Aj')
else:
    response_data = 'Nothing to update!'
    return HttpResponse(response_data, content_type="text/plain")
response_data = ''
try:
*(Here, I would like to insert the incoming dictionary (radinputV) rows into my model skills)*
except:
    response_data = 'Ouch! Something went wrong!'
return HttpResponse(response_data, content_type="text/plain")

下面是技能模型:

class skills(models.Model):
id = models.CharField(max_length=300,blank=True)
skill = models.CharField(max_length=300,blank=True)
rating = models.CharField(max_length=300,blank=True)
ordernum = models.CharField(max_length=300,blank=True)

这是从模板传入的 json 字典与技能模型中字段的映射 - 技能为轴, 评级是价值, ordernum 是订单

在视图中:

radinputV=[]
radinputV = request.POST.get('radinput_Aj')
radinputV1 = request.POST.get('radinput_Aj[0][value]')

我可以使用上面的符号访问字典项目。

但是,如果我使用上面的表示法,我需要为 Dictionary 对象 V 编写 21 条这样的语句(7 行 * 3 列)...有没有简单的方法?

如何处理从模板 JSON 对象 - Django 视图向 mysql table 插入多行的情况?

已编辑,最近尝试使用 get 方法。

您不能通过标准表单参数发送这样的数据结构。发送前需要先序列化为JSON,然后在视图中反序列化

更改data参数如下:

data: {
    'radinput_Aj' : JSON.stringify(fun()),
    'csrfmiddlewaretoken': $("input[name=csrfmiddlewaretoken]").val()
},

和视图:

if request.method == "POST":
    data = request.POST.get('radinput_Aj')
    radinputV = json.loads(data)