Django rest框架序列化程序和数据表,带有kwargs请求

时间:2019-02-14 11:35:26

标签: datatables django-rest-framework django-serializer

我正在开发一个模板,其中包含一个由两个关键字(kwarg)生成的报告表

我正在使用带有数据表的Django 休息框架 我将使用kwargs过滤查询集,而以下内容则无法过滤。

我知道以下代码不正确,我尝试解释一下我的目标:

Views.py

from rest_framework import viewsets
from myapp.serializers import ReportSerializer

def index(request, *args, **kwargs):  
    return render(request, 'report.html')


class ReportViewSet(viewsets.ModelViewSet):
    queryset = Reports.objects.all().filter(idl = kwargs['idl', idr = kwargs['idr'])
    serializer_class = ReportSerializer

urls.py

from rest_framework import routers

    router = routers.DefaultRouter()
    router.register(r'reports', views.ReportViewSet, base_name = 'ReportViewSet')

    urlpatterns = [
        url('^api/', include(router.urls)),
        url('', views.index, name='reports'),

serializers.py

from rest_framework import serializers

class ReportSerializer(serializers.ModelSerializer):
    id = serializers.IntegerField(read_only=True)
    class Meta:
        model = Reports
        fields = (
            'id','idl','idr','idz','time_s','product',)

report.html

var table= $('#mytable').DataTable({
              'ajax': {
              "url": '/api/reports/?format=datatables',
              "dataType": "json",
                "contentType": 'application/json; charset=utf-8',
              },
              'columns': [
                  {"data": 'idl'},
                  {"data": 'idr'},
                  {"data": 'idz'},
                  {"data": 'time_s'},
          {"data": 'product'},
              ],
      });

我认为必须在

中对其进行修改
          "url": '/api/reports/idl/idr/?format=datatables',

0 个答案:

没有答案