Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django将两个json对象组合成一个新数组_Json_Django - Fatal编程技术网

Django将两个json对象组合成一个新数组

Django将两个json对象组合成一个新数组,json,django,Json,Django,我有两个json对象,如下所示 身份证号码:1 Id-2 我正在尝试将这两个数据合并到画布上 我能够检索单个文件,但无法将它们组合在一起 def loadDrawing(request): """ Function to load the drawing with drawingID if it exists.""" try: # Getting JSON object string of saved d

我有两个json对象,如下所示

身份证号码:1

Id-2

我正在尝试将这两个数据合并到画布上 我能够检索单个文件,但无法将它们组合在一起

def loadDrawing(request):
    """ Function to load the drawing with drawingID if it exists."""
    try:
        # Getting JSON object string of saved drawing.
        drawingJSONData = Drawing.objects.get(id = 1).drawingJSONText

        # drawingJSONData1 = Drawing.objects.get(id=1).drawingJSONText
       # drawingJSONData2 = Drawing.objects.get(id=2).drawingJSONText

        # Seding context with appropriate information
        context = {
            "loadIntoJavascript" : True,
            "JSONData" : drawingJSONData

        }
        # Editing response headers and returning the same
        response = modifiedResponseHeaders(render(request, 'MainCanvas/index.html', context))
        return response
我在django的模特

class Drawing(models.Model):
    drawingJSONText = models.TextField(null = True)
My.js文件加载图形并将其从服务器解析为JSON对象,并将加载的点推送到数组中

 // Checking if the drawing to be loaded exists
    if (document.getElementById('JSONLoadData') != null)
    {

        // Parsing the loaded drawing from server to a JSON Object
        var loadedData = JSON.parse(JSONLoadData.value)

        // Iterating through all the points in the loaded drawing
        for(let i = 0; i < loadedData['points'].length; i++)
        {
            // Saving the point and drawing the same in the svg canvas
            const point = svg.append('circle')
                             .attr('cx', loadedData['points'][i]['x'])
                             .attr('cy', loadedData['points'][i]['y'])
                             .attr('r', loadedData['points'][i]['r'])
                             .style('fill', loadedData['points'][i]['color']);

            // Pushing the point inside points array
            points.push(point);
        }

        // Iterating through all the lines in the loaded drawing
        for(let i = 0; i < loadedData['lines'].length; i++)
        {
            // Saving the line and drawing the same in the svg canvas
            const line = svg.append('line')
                            .attr('x1', loadedData['lines'][i]['x1'])
                            .attr('y1', loadedData['lines'][i]['y1'])
                            .attr('x2', loadedData['lines'][i]['x2'])
                            .attr('y2', loadedData['lines'][i]['y2'])
                            .attr('stroke-width', loadedData['lines'][i]['strokeWidth'])
                            .style('stroke', loadedData['lines'][i]['strokeColor']);

            // Pushing the line inside lines array
            lines.push(line);
        }

    }

});
如何根据项目筛选数据 假设我有三个数据集

1st one contains project = a
2nd one contains project = b
3rd one contains project = a
4th one contains project = a
如何通过过滤数据
Drawing.objects.filter(project=a)

然后根据查询集,我有三个数据点,相应的数据如上图所示绘制在画布上

我不完全确定这是您想要的,但您是否尝试将id-1和id-2结合起来?如果我认为我理解您正在尝试做什么,那么在这种情况下使用+运算符是否有效

drawingJSONData1 = json.loads(Drawing.objects.get(id=1).drawingJSONText)
drawingJSONData2 = json.loads(Drawing.objects.get(id=1).drawingJSONText)

drawingJSONData = dict()
drawingJSONData["points"] = drawingJSONData1["points"]+drawingJSONData1["points"]
drawingJSONData["lines"] = drawingJSONData2["lines"]+drawingJSONData2["lines"]
通过上面的示例,您将得到:

{'points': [{'x': 109, 'y': 286, 'r': 1, 'color': 'black'},
  {'x': 108, 'y': 285, 'r': 1, 'color': 'black'},
  {'x': 106, 'y': 282, 'r': 1, 'color': 'black'},
  {'x': 103, 'y': 276, 'r': 1, 'color': 'black'},
  {'x': 524, 'y': 343, 'r': 1, 'color': 'black'},
  {'x': 523, 'y': 342, 'r': 1, 'color': 'black'},
  {'x': 521, 'y': 339, 'r': 1, 'color': 'black'},
  {'x': 520, 'y': 334, 'r': 1, 'color': 'black'},
  {'x': 514, 'y': 319, 'r': 1, 'color': 'black'}],
 'lines': [{'x1': 109,
   'y1': 286,
   'x2': 108,
   'y2': 285,
   'strokeWidth': '2',
   'strokeColor': 'black'},
  {'x1': 108,
   'y1': 285,
   'x2': 106,
   'y2': 282,
   'strokeWidth': '2',
   'strokeColor': 'black'},
  {'x1': 106,
   'y1': 282,
   'x2': 103,
   'y2': 276,
   'strokeWidth': '2',
   'strokeColor': 'black'},
  {'x1': 524,
   'y1': 343,
   'x2': 523,
   'y2': 342,
   'strokeWidth': '2',
   'strokeColor': 'black'},
  {'x1': 523,
   'y1': 342,
   'x2': 521,
   'y2': 339,
   'strokeWidth': '2',
   'strokeColor': 'black'},
  {'x1': 521,
   'y1': 339,
   'x2': 520,
   'y2': 334,
   'strokeWidth': '2',
   'strokeColor': 'black'},
  {'x1': 520,
   'y1': 334,
   'x2': 514,
   'y2': 319,
   'strokeWidth': '2',
   'strokeColor': 'black'}]}

编辑:在json中添加了get包装。加载将字符串转换为Python对象,因为我不知道这是什么类型的字段,并且给出了所看到的错误。

我得到了一个类型错误“字符串索引必须是整数”我编辑了一点答案。您可能需要将这些字段中的字符串转换为python对象,以使其正常工作。编辑您的问题以显示绘图模型的外观。我现在没有收到错误,但我得到一个空白页,画布上没有加载坐标。猜您需要转换回字符串吗?在您的上下文中,尝试
“JSONData”:json.dumps(drawingJSONData),
有效。谢谢
1st one contains project = a
2nd one contains project = b
3rd one contains project = a
4th one contains project = a
drawingJSONData1 = json.loads(Drawing.objects.get(id=1).drawingJSONText)
drawingJSONData2 = json.loads(Drawing.objects.get(id=1).drawingJSONText)

drawingJSONData = dict()
drawingJSONData["points"] = drawingJSONData1["points"]+drawingJSONData1["points"]
drawingJSONData["lines"] = drawingJSONData2["lines"]+drawingJSONData2["lines"]
{'points': [{'x': 109, 'y': 286, 'r': 1, 'color': 'black'},
  {'x': 108, 'y': 285, 'r': 1, 'color': 'black'},
  {'x': 106, 'y': 282, 'r': 1, 'color': 'black'},
  {'x': 103, 'y': 276, 'r': 1, 'color': 'black'},
  {'x': 524, 'y': 343, 'r': 1, 'color': 'black'},
  {'x': 523, 'y': 342, 'r': 1, 'color': 'black'},
  {'x': 521, 'y': 339, 'r': 1, 'color': 'black'},
  {'x': 520, 'y': 334, 'r': 1, 'color': 'black'},
  {'x': 514, 'y': 319, 'r': 1, 'color': 'black'}],
 'lines': [{'x1': 109,
   'y1': 286,
   'x2': 108,
   'y2': 285,
   'strokeWidth': '2',
   'strokeColor': 'black'},
  {'x1': 108,
   'y1': 285,
   'x2': 106,
   'y2': 282,
   'strokeWidth': '2',
   'strokeColor': 'black'},
  {'x1': 106,
   'y1': 282,
   'x2': 103,
   'y2': 276,
   'strokeWidth': '2',
   'strokeColor': 'black'},
  {'x1': 524,
   'y1': 343,
   'x2': 523,
   'y2': 342,
   'strokeWidth': '2',
   'strokeColor': 'black'},
  {'x1': 523,
   'y1': 342,
   'x2': 521,
   'y2': 339,
   'strokeWidth': '2',
   'strokeColor': 'black'},
  {'x1': 521,
   'y1': 339,
   'x2': 520,
   'y2': 334,
   'strokeWidth': '2',
   'strokeColor': 'black'},
  {'x1': 520,
   'y1': 334,
   'x2': 514,
   'y2': 319,
   'strokeWidth': '2',
   'strokeColor': 'black'}]}