删除表行django休息框架

时间:2017-12-20 09:16:48

标签: django datatables django-rest-framework

我的数据表中有两个按钮,我试图用来删除和编辑一行。

在我ModelViewSet我试图做这样的事情:

from django.shortcuts import get_object_or_404
from rest_framework import viewsets, filters, status
from rest_framework.decorators import detail_route
from rest_framework.response import Response

from beaconpro.company.models.organization_model import OrganizationModel
from beaconpro.company.restapi.serializers.organization_model_serializer import OrganizationModelSerializer
from beaconpro.company.restapi.pagination import StandardResultsOffsetPagination


class OrganizationViewSet(viewsets.ModelViewSet):
    queryset = OrganizationModel.objects.all()
    serializer_class = OrganizationModelSerializer
    filter_backends = (filters.DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
    filter_fields = ('id', 'organization_name')
    ordering_fields = ('id', 'organization_name', 'delete_organization_name')
    search_fields = ('id', 'organization_name', 'delete_organization_name')
    pagination_class = StandardResultsOffsetPagination

    def update(self, request, *args, **kwargs):
        return self.create(request, args, kwargs)

    def destroy(self, request, *args, **kwargs):
        organization = get_object_or_404(OrganizationModel, pk=kwargs.get('pk'))
        organization.delete()
        return super(OrganizationViewSet, self).destroy(request, *args, **kwargs)

    @detail_route(methods=['update'])
    def edit_organization_name(self, request, pk=None):
        pass

    @detail_route(methods=['delete'])
    def delete_organization_name(self, request, pk):
        organization = get_object_or_404(OrganizationModel, pk=pk)
        organization.delete()
        return Response(status=status.HTTP_200_OK)

所以我使用delete_organization_namedata传递到datatable到这样的按钮:

$('#searchCompanyList').DataTable( {
    processing: true,
    serverSide: true,
    searchDelay: 1000,
    order: [[ 1, "desc" ]],
    orderMulti: false,
    pageLength: 10,
    ajax: {
      url: '/api/organization/',
      data: function(data) {
        data.limit = data.length;
        data.offset = data.start;
        data.search = data.search['value'];
        if (data.order[0]['dir'] == 'asc') {
            data.ordering = data.columns[data.order[0]['column']].name;
        } else {
            data.ordering = '-' + data.columns[data.order[0]['column']].name;
        }
      },
      dataFilter: function(data) {
        var json = jQuery.parseJSON( data );
        json.recordsTotal = json.count;
        json.recordsFiltered = json.count;
        json.data = json.results;
        return JSON.stringify( json ); // return JSON string
      }
    },
    columns: [
        {
            data: "organization_name",
            name: "organization_name"
        },
        {
            data: "edit_organization_name",
            render: function ( data, type, row, meta ) {
                return '<a type="button" class="btn btn-sm btn-info" href="' + data + '">Edit</a>';
            }
        },
        {
            data: "delete_organization_name",
            render: function ( data, type, row, meta ) {
                return '<a type="button" class="btn btn-sm btn-danger" href="' + data + '">Remove</a>';
            }
        }
    ]

  });

现在我将所有数据传递给数据表的url/api/organization/,所以我正在尝试构建一个url来删除一行像这样的

urlpatterns = [
url(
    r'^create-company/$',
    login_required(views.OrganizationCreateView.as_view(), login_url='/'),
    name='create-company'
),
url(
    r'^organization/(?P<pk>\d+)/edit/$',
    login_required(apiviews.OrganizationViewSet, login_url='/'),
    name='organization-edit'
),
url(
    r'^organization/(?P<pk>\d+)/delete/$',
    login_required(apiviews.OrganizationViewSet, login_url='/'),
    name='organization-delete'
)

但我得到的只是这个错误:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/create-company/undefined

所以网址不好,而且我没有掌握如何正确配置,所以任何人都可以建议做什么以及如何实现我的按钮删除一个表格行。

0 个答案:

没有答案