简体   繁体   English

django-rest-framework-datatables JSON 不会加载

[英]django-rest-framework-datatables JSON will not Load

I am using django-rest-framework-datatables and following the directions but can not get to work.我正在使用 django-rest-framework-datatables 并按照说明进行操作,但无法开始工作。

I have followed this:我遵循了这个:

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

I am getting the warning "DataTables warning: table id=transactions - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1 "我收到警告“DataTables 警告:table id=transactions - 无效的 JSON 响应。有关此错误的更多信息,请参阅http://datatables.net/tn/1

my URLS are in an app called "custom"我的 URL 位于名为“自定义”的应用程序中

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'),

]

my views:我的观点:

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


def exber_index(request):

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

the model:该模型:

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 )

the serializer:序列化器:

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',)

and this is the table:这是表:

<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>

I suspect it is in a path somewhere?我怀疑它在某处的路径中? I have tried many combinations.我尝试了很多组合。

thank you.谢谢你。

the error appears to have been in the serializer not having the "id" field included.错误似乎出现在序列化程序中,没有包含“id”字段。 I just added this.我刚刚添加了这个。

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

It is because you have this line in your serializer:这是因为您的序列化程序中有这一行:

datatables_always_serialize = ('id',)

But you do not have included the id field in your serializer fields list.但是您没有在序列化程序fields列表中包含id字段。

So, to solve the issue, either remove the datatables_always_serialize line or add id to your serializer fields.因此,要解决此问题,请删除datatables_always_serialize行或将id添加到序列化程序字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM