![](/img/trans.png)
[英]How to write a test for 'delete' operation in Django rest framework
[英]How to perform UPDATE and DELETE operation in Django rest framework
我想在Django rest框架中執行UPDATE和DELETE操作,我做了GET和POST操作。 請幫我做UPDATE和DELETE操作。
views.py
class SettingprofileViews(viewsets.ModelViewSet):
queryset = Setting_profile.objects.all()
serializer_class = SettingprofileSerializer
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
class SettingprofileSerializer(serializers.ModelSerializer):
class Meta:
model = Setting_profile
fields = '__all__'
urls.py
router = routers.DefaultRouter()
router.register('api/settingprofile', views.SettingprofileViews)
urlpatterns = [
path('', include(router.urls)),
]
ModelViewSet已經為PUT和DELETE HTTP方法實現了操作。
請參閱: https : //www.django-rest-framework.org/api-guide/viewsets/#modelviewset
這意味着如果您執行HTTP請求:
DELETE / api / settingprofile / 1
restframework將調用destroy(request,pk = 1)函數,以便從Setting_profile表中刪除id = 1的行。
PUT / api / settingprofile / 2
restframework將調用update(request,pk = 2)函數並檢查請求參數,因此Setting_profile表中id = 2的行將相應地更改為新數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.