简体   繁体   English

通过 Django REST API 删除对象

[英]delete object through Django REST API

I'm trying to delete "Product" object using Django REST API but don't know how to do this.我正在尝试使用 Django REST API 删除“产品”对象,但不知道如何执行此操作。

serializer:序列化器:

class ProductSerializer(serializers.ModelSerializer):
    class Meta:
        model = Product
        fields = ('id', 'product_name', 'measure', 'barcode')

I can create product using this function我可以使用此功能创建产品

def create_product(request):
    data = request.POST
    serializer = ProductSerializer(data=data)
    if serializer.is_valid():
        serializer.save()

But I don't know how to delete但是不知道怎么删除

There is no serializer.delete() method.没有 serializer.delete() 方法。

You can do that using query set:您可以使用查询集来做到这一点:

@api_view(["DELETE"])
def product_delete_rest_endpoint(request, product_id):
    Product.objects.get(id=product_id).delete()
    return Response()

If your view(set) inherits from DestroyModelMixin , or a viewset which inherit's from it, eg ModelViewSet , http DELETE is supported out of the box.如果您的视图(集)继承自DestroyModelMixin或继承自它的视图集,例如ModelViewSet ,则支持开箱即用的 http DELETE You can test it with curl, for example curl -X DELETE "http://localhost:8000/your-api/products/<product-id>" .您可以使用 curl 对其进行测试,例如curl -X DELETE "http://localhost:8000/your-api/products/<product-id>"

First of all you should know how work with Router and ModelViewSet and GenericViewSet class from django rest_framework and instead of Product app let assume that you have Customer app.首先,您应该知道如何使用 django rest_framework 中的 Router 和 ModelViewSet 以及 GenericViewSet 类,而不是 Product 应用程序,假设您有 Customer 应用程序。 you should inheritance your class viewset from viewsets.ModelViewSet or viewsets.GenericViewSet and mixins.DestroyModelMixin in views.py like blow你应该从viewsets.ModelViewSet或viewsets.GenericViewSet和mixins.DestroyModelMixin在views.py中继承你的类视图集,就像打击一样

class CustomerViewSet(viewsets.GenericViewSet, mixins.DestroyModelMixin):

or或者

class CustomerViewSet(viewsets.ModelViewSet):

and define serilaizer class for that viewset like this并像这样为该视图集定义 serilaizer 类

class CustomerViewSet(viewsets.GenericViewSet, mixins.DestroyModelMixin):
      serilaizer_class = CustomerSerializer

then you should define router for all request to api in your appName folder and you should create urls.py file in that folder like below那么你应该在你的 appName 文件夹中为所有对 api 的请求定义路由器,你应该在该文件夹中创建 urls.py 文件,如下所示

you can see customer app folder in my project and urls.py您可以在我的项目和 urls.py 中看到客户应用程序文件夹

in urls.py define router and url patterns like this在 urls.py 中像这样定义路由器和 url 模式

app_name variable define the name of url used by revers function app_name 变量定义了 revers 函数使用的 url 的名称

go to the urls.py in app folder (app/urls.py) and define path like below " you should define a path to refer to customer/urls.py .. we create customer/urls.py in previous step "转到应用程序文件夹(app/urls.py)中的 urls.py 并定义如下路径“您应该定义一个路径来引用 customer/urls.py ..我们在上一步中创建了 customer/urls.py

Define path in (app/urls.py)在 (app/urls.py) 中定义路径

OK .. run your project in browser and type your localhost address http://(your localhost address)/api/customer/customers/1/ with DELETE request ( NOT POST, GET or PATCH request) OK .. 在浏览器中运行你的项目并输入你的本地主机地址 http://(你的本地主机地址)/api/customer/customers/1/ 和 DELETE 请求(不是 POST、GET 或 PATCH 请求)

here is some link about router and viewset这是一些关于路由器和视图集的链接

Router : https://www.django-rest-framework.org/api-guide/routers/路由器: https : //www.django-rest-framework.org/api-guide/routers/

ModelViewSet: https://www.django-rest-framework.org/api-guide/viewsets/#modelviewset模型视图集: https : //www.django-rest-framework.org/api-guide/viewsets/#modelviewset

Generic Viewset: https://www.django-rest-framework.org/api-guide/viewsets/#genericviewset通用视图集: https ://www.django-rest-framework.org/api-guide/viewsets/#genericviewset

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM