如何在Django rest框架中执行UPDATE和DELETE操作

jhdbpxl9  于 2022-12-30  发布在  Go
关注(0)|答案(1)|浏览(130)

我想在Django框架中执行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)),
]
mrzz3bfm

mrzz3bfm1#

ModelViewSet已实现PUT和DELETE HTTP方法的操作。
See: https://www.django-rest-framework.org/api-guide/viewsets/#modelviewset
这意味着如果您执行HTTP请求:

    • 删除/api/设置配置文件/1**
  • restframework * 将调用 * destroy(request,pk = 1)* 函数,以便从Setting_profile表中删除id = 1的行。
    • PUT/应用程序接口/设置配置文件/2**
  • restframework * 将调用 * update(request,pk = 2)* 函数并检查 * request * 参数,因此 * Setting_profile * 表中id = 2的行将相应地更改为新数据。

相关问题