[英]UPDATE and DELETE Operation in Python Django REST
I am trying to implement simple CRUD operation in Django Rest. 我正在尝试在Django Rest中实现简单的CRUD操作。 I have successfully created,
GET
and POST
. 我已经成功创建了
GET
和POST
。 Now, I want to update the row data with PUT
and delete a row with DELETE
. 现在,我想使用
PUT
更新行数据,并使用DELETE
删除行。
Currently, my table looks like :- 目前,我的桌子看起来像:-
id item_1 item_2 item_3 date_created date_modified
1 test1 test2 test3 16-12-2017 16-12-2017
2 ex1 ex2 ex3 16-12-2017 16-12-2017
API End point :- localhost/itemlist
API端点:-
localhost/itemlist
If we GET localhost/itemlist
it returns the list of items. 如果我们
GET localhost/itemlist
它将返回项目列表。
If we POST localhost/itemlist
with fields item_1
, item_2
, item_3
, it creates the row. 如果我们使用字段
item_1
, item_2
, item_3
POST localhost/itemlist
item_3
,它将创建该行。
Now, I want to use PUT
to update a particular row. 现在,我想使用
PUT
更新特定行。 Like, if I want to update the value of item_3
where item_1 == "test1"
就像,如果我想更新
item_3
的值,其中item_1 == "test1"
DELETE should delete the row where item_1 == ex1
DELETE应该删除
item_1 == ex1
的行
urls.py urls.py
from django.conf.urls import url, include
from rest_framework.urlpatterns import format_suffix_patterns
from .views import ItemView
urlpatterns = {
url(r'^itemlists/$', ItemView.as_view(), name="create"),
}
urlpatterns = format_suffix_patterns(urlpatterns)
views.py : views.py:
from rest_framework import generics
from .serializers import itemlistSerializer
from .models import itemlist
class ItemView(generics.ListCreateAPIView):
queryset = itemlist.objects.all()
serializer_class = itemlistSerializer
def perform_create(self, serializer):
serializer.save()
serializers.py serializers.py
from rest_framework import serializers
from .models import itemlist
class itemlistSerializer(serializers.ModelSerializer):
class Meta:
"""Meta class to map serializer's fields with the model fields."""
model = itemlist
fields = ('id', 'item_1', 'item_2', 'item_3', 'date_created', 'date_modified')
read_only_fields = ('date_created', 'date_modified')
models.py models.py
from django.db import models
class itemlist(models.Model):
"""This class represents the itemlist model."""
item_1 = models.CharField(max_length=255, blank=True, unique=False)
item_2 = models.CharField(max_length=255, blank=True, unique=False)
item_3 = models.CharField(max_length=255, blank=True, unique=False)
date_created = models.DateTimeField(auto_now_add=True)
date_modified = models.DateTimeField(auto_now=True)
def __str__(self):
"""Return a human readable representation of the model instance."""
return "{}".format(self.item_1)
Use django viewsets: 使用Django视图集:
viewsets.py viewsets.py
class ListItemViewSet(viewsets.ModelViewSet):
serializer_class = itemlistSerializer
queryset = itemlist.objects.all()
http_method_names = ['get', 'post', 'put', 'delete', 'option']
def get_queryset(self):
request_user = self.request.user
if not request_user.is_superuser:
return self.queryset.filter(owner=request_user)
urls.py urls.py
router = routers.DefaultRouter()
router.register(listitem', ListItemViewSet)
urlpatterns = [
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
]
Then you should be able to send put and delete requests. 然后,您应该能够发送放置和删除请求。 This works for Django 1.9x.
这适用于Django 1.9x。
Just one more thing: Please read the Python Style Guide . 还有一件事:请阅读Python样式指南 。 We normally use CamelCase for class names.
我们通常使用CamelCase作为类名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.