【问题标题】:django-rest-framework-datatables JSON will not Loaddjango-rest-framework-datatables JSON 不会加载
【发布时间】:2019-07-11 08:39:07
【问题描述】:

我正在使用 django-rest-framework-datatables 并按照说明进行操作,但无法开始工作。

我遵循了这个:

https://django-rest-framework-datatables.readthedocs.io/en/latest/index.html

我收到警告“DataTables 警告:表 id=transactions - 无效 JSON 响应。有关此错误的更多信息,请参阅 http://datatables.net/tn/1

我的网址在一个名为“自定义”的应用中

app_name = 'custom'

router = routers.DefaultRouter()
router.register(r'exber', views.ExberViewSet)

urlpatterns = [

    url('^exber/api/', include(router.urls)),
    url('^exber/', views.exber_index, name='exber'),

]

我的观点:

class ExberViewSet(viewsets.ModelViewSet):
    queryset = Exber_transactions.objects.all()
    serializer_class = ExberSerializer


def exber_index(request):

    return render(request, 'custom/exber/transactions.html)

型号:

class Exber_transactions ( models.Model ):

    date = models.DateField ( null = True, blank = True )
    desc = models.CharField ( max_length = 100 )
    activity = models.CharField ( max_length = 100 )
    qty = models.DecimalField ( max_digits = 12, decimal_places = 3, default = 0 )
    price = models.DecimalField ( max_digits = 12, decimal_places = 2, default = 0 )
    accrued_int = models.DecimalField ( max_digits = 12, decimal_places = 3, default = 0 )
    amount = models.DecimalField ( max_digits = 12, decimal_places = 2, default = 0 )

序列化器:

class ExberSerializer(serializers.ModelSerializer):
    id = serializers.IntegerField ( read_only = True )

    class Meta:
        model = Exber_transactions
        fields = ('date','desc', 'activity','qty', 'price', 'accrued_int', 'amount')
        datatables_always_serialize = ('id',)

这是桌子:

<table id="transactions" class="table table-striped table-bordered" style="width:100%" data-server-side="true" data-ajax="custom/api/exber?format=datatables">

      <thead>
        <tr>
          <th data-data="date">date</th>
          <th data-data="desc">desc</th>
          <th data-data="activity">activity</th>
          <th data-data="qty">qty</th>
          <th data-data="price">price</th>
          <th data-data="accrued_int">accrued_int</th>
          <th data-data="amount">amount</th>
        </tr>
      </thead>

    </table>


<script>
  $(document).ready(function() {
      $('#transactions').DataTable();
  });
</script>

我怀疑它在某处的路径中?我尝试了很多组合。

谢谢。

【问题讨论】:

    标签: django datatables


    【解决方案1】:

    错误似乎出现在没有包含“id”字段的序列化程序中。我刚刚添加了这个。

    fields = ('id', 'date','desc', 'activity','qty', 'price', 'accrued_int', 'amount'
    

    【讨论】:

      【解决方案2】:

      这是因为您的序列化程序中有这一行:

      datatables_always_serialize = ('id',)
      

      但是您没有在序列化程序fields 列表中包含id 字段。

      因此,要解决此问题,请删除 datatables_always_serialize 行或将 id 添加到您的序列化器字段中。

      【讨论】:

        猜你喜欢
        • 2019-08-10
        • 1970-01-01
        • 2012-12-07
        • 1970-01-01
        • 2018-08-21
        • 2019-08-19
        • 2023-03-14
        • 2019-03-17
        • 2021-08-03
        相关资源
        最近更新 更多