我想在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)),
]
答案 0 :(得分:1)
您可以在 PUT
端点中进行这些操作(PATCH
,DELETE
和api/student/1234/
),其中 1234
是要删除或更新的实例的 PK
。
您可以在此处DefaultRouter--[DRF-DOC]