如何将数据从Django传递到Shield UI Grid?

时间:2017-04-23 16:07:43

标签: json django gridview serialization shieldui

我是使用Django和Shield UI的新手,我正在尝试做的是尝试获取shieldGrid的数据,请求从服务器获取远程数据。我有下一个代码。

这是我的.js

$("#id_table_diagnosticos").shieldGrid({
                  dataSource: {
                      remote: {
                          read: {
                              url: "/atender/ListadoDiagnosticos/",
                              dataType: "json",
                              operations: ["sort", "skip", "take"],
                              data: function (params) {
                                  var odataParams = {};
                                  if (params.sort && params.sort.length) {
                                      odataParams["$orderby"] = window.orderFields[params.sort[0].path].path + (params.sort[0].desc ? " desc" : "");
                                  }
                                  if (params.skip != null) {
                                      odataParams["skip"] = params.skip;
                                  }
                                  if (params.take != null) {
                                      odataParams["top"] = params.take;
                                  }
                                  return odataParams;
                              }
                          }
                      },
                      schema: {
                          data: "fields",
                          total: function (result) {
                              return result["odata.count"];
                          },
                          fields: window.orderFields = {
                            //  "pk": { path: "pk" },
                              "descripcion": { path: "descripcion" },
                              "codigo": { path: "codigo" },
                          }
                      }
                  },
                  paging: true,
                  sorting: true,
                  columns: [
                    //  { field: "pk", title: "ID", width: 80 },
                      { field: "descripcion", title: "Descripción", width: 180 },
                      { field: "codigo", title: "Código", width: 100 },
                  ]
              });
          });

我的model.py是

class ParametrosDiagnosticos(models.Model):
descripcion=models.CharField(max_length=1000)
codigo=models.CharField(max_length=100)
sexo=models.CharField(max_length=10,default='Ambos')
estado=models.CharField(max_length=100, default='Activo')
estado_logico=models.IntegerField(default=1)

和我的view.py

def ListadoDiagnosticos(request):
if request.user.is_authenticated:
    if request.method=='GET' and request.is_ajax():
        objeto=ParametrosDiagnosticos.objects.all()
        data = serializers.serialize('json', objeto, fields=('pk','descripcion','codigo'))
        return JsonResponse(data,safe=False);

对服务器的请求没问题,问题是当我返回数据时,我在浏览器的控制台上出现了下一个错误。

shieldui-all.min.js:4 Uncaught TypeError: Cannot read property 'map' of undefined
at e (shieldui-all.min.js:4)
at init.fields (shieldui-all.min.js:5)
at init.process (shieldui-all.min.js:5)
at init._success (shieldui-all.min.js:5)
at e (bundled.js:2)
at Object.z.func.i.success (shieldui-all.min.js:4)
at j (bundled.js:2)
at Object.fireWith [as resolveWith] (bundled.js:2)
at x (bundled.js:5)
at XMLHttpRequest.b (bundled.js:5)

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

确保您的网格dataSource.schema配置正确。有关它的文档可以在here找到。

例如,如果从服务器返回的数据包含对象列表,则无需设置其data属性。