[英]How to perform UPDATE and DELETE operation in Django rest framework
I want to perform UPDATE and DELETE operation in Django rest framework, I did GET and POST operation. 我想在Django rest框架中执行UPDATE和DELETE操作,我做了GET和POST操作。 Please help me to do UPDATE and DELETE operation. 请帮我做UPDATE和DELETE操作。
views.py views.py
class SettingprofileViews(viewsets.ModelViewSet):
queryset = Setting_profile.objects.all()
serializer_class = SettingprofileSerializer
models.py models.py
class Setting_profile(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 serializers.py
class SettingprofileSerializer(serializers.ModelSerializer):
class Meta:
model = Setting_profile
fields = '__all__'
urls.py urls.py
router = routers.DefaultRouter()
router.register('api/settingprofile', views.SettingprofileViews)
urlpatterns = [
path('', include(router.urls)),
]
The ModelViewSet already implements actions for PUT and DELETE HTTP methods. ModelViewSet已经为PUT和DELETE HTTP方法实现了操作。
See: https://www.django-rest-framework.org/api-guide/viewsets/#modelviewset 请参阅: https : //www.django-rest-framework.org/api-guide/viewsets/#modelviewset
It means that if you perform HTTP requests: 这意味着如果您执行HTTP请求:
DELETE /api/settingprofile/1 DELETE / api / settingprofile / 1
The restframework will call destroy(request, pk=1) function in order to remove row with id=1 from Setting_profile table. restframework将调用destroy(request,pk = 1)函数,以便从Setting_profile表中删除id = 1的行。
PUT /api/settingprofile/2 PUT / api / settingprofile / 2
The restframework will call update(request, pk=2) function and inspect request parameter, so the row with id=2 in Setting_profile table will be changed accordingly to a new data. restframework将调用update(request,pk = 2)函数并检查请求参数,因此Setting_profile表中id = 2的行将相应地更改为新数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.