如何在Django rest框架中执行Update,Delete操作

时间:2019-06-11 17:16:32

标签: python django django-rest-framework

我想在Django rest框架中执行Update和Delete操作,我做了Get和Post操作。我是django的新手,请帮助我进行更新和删除操作。

views.py

class StudentViews(viewsets.ModelViewSet):
    queryset = Student.objects.all()
    serializer_class = StudentSerializer

models.py

class Student(models.Model):
    name = models.CharField(max_length=255, blank=True, null=True)
    contact_number = models.CharField(max_length=12, blank=True, null=True)
    email = models.EmailField(max_length=100, blank=True, null=True)
    address = models.CharField(max_length=500, blank=True, null=True)

serializers.py

class StudentSerializer(serializers.ModelSerializer):

    class Meta:
        model = Student
        fields = '__all__'

urls.py

router = routers.DefaultRouter()
router.register('api/student', views.StudentViews)

urlpatterns = [
    path('', include(router.urls)),
]

1 个答案:

答案 0 :(得分:1)

您可以在 PUT 端点中进行这些操作(PATCHDELETEapi/student/1234/),其中 1234 是要删除或更新的实例的 PK

您可以在此处DefaultRouter--[DRF-DOC]

引用更多有关已创建的端点的信息。