如何在Django Rest框架中更新和删除数据

时间:2019-02-18 13:19:58

标签: django-rest-framework

我在DRF中执行CRUD,并且2个操作(Get,Post)运行正常,但在PUT和DELETE操作中出现错误。 Put()缺少1个必需的位置参数:'id',并且删除时发生相同的错误。 delete()缺少1个必需的位置参数:'id',当我尝试通过在URL中传递id来获取数据时,出现此错误“ get()得到了意外的关键字参数'id'”

我更改了网址格式,但收到相同的错误

  

views.py

from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from .models import Employee
from .serializer import EmployeeSerializer

class employeeList(APIView):

    def get(self, request):
        employee1 = Employee.objects.all()
        serializer1 = EmployeeSerializer(employee1 , many = True)
        return Response(serializer1.data)

    def post(self, request):
        employee1 = request.data.get("")
        serializer1 = EmployeeSerializer(data=request.data, 
partial=True)
        if serializer1.is_valid(raise_exception=True):
            employee_saved = serializer1.save()
        return Response({"success": "Employee '{}' created 
successfully".format(employee_saved.first_name)})
    def put(self , request, id):
        employee = get_object_or_404(Employee, id=id)
        data = request.data.get("")
        serializer1 = EmployeeSerializer(instance = employee , 
data=data, partial=True)
        if serializer1.is_valid(raise_exception=True):
            saved_employee = serializer1.save()
        return Response({"success":"Employee '{}' updated 
successfully".format(saved_employee.first_name)})
    def delete(self, request, id):
        employee = get_object_or_404(Employee, id=id)
        employee.delete()
        return Response({"message":"Employee '{}' deleted 
successfully".format(employee.first_name)}, status=204)
  

serializer.py

from rest_framework import serializers
from .models import Employee

class EmployeeSerializer(serializers.ModelSerializer):

    class Meta:
        model = Employee
        fields = '__all__'
    def create(self, validated_data):
        return Employee.objects.create(**validated_data)
    def update(self, instance, validated_data):

instance.first_name=validated_data.get('first_name',instance.first_name)
        instance.last_name = 
validated_data.get('last_name',instance.last_name)
        instance.employee_id = 
validated_data.get('employee_id',instance.employee_id)
        instance.save()
        return instance
  

urls.py

from django.conf.urls import url
from django.contrib import admin
from webapp import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^employee/', views.employeeList.as_view()),
    url(r'^employees/(?P<id>[0-9]+)$', views.employeeList.as_view())
]

我希望看到成功消息,但会收到错误消息

1 个答案:

答案 0 :(得分:0)

您必须指出每种网址模式将要管理的方法:

from django.conf.urls import url
from django.contrib import admin
from webapp import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^employee/', views.employeeList.as_view({'get': 'list', 'post': 'create'})),
    url(r'^employees/(?P<id>[0-9]+)$', views.employeeList.as_view({'get': 'retrieve', 'put': 'update', 'delete': 'destroy'}))
]

'get': 'list'表示方法GET将由视图的类方法list()管理。