所以我跟着DjangoREST framewok网站上的Quickstart Guide,最后得到了以下代码:
serializers.py:
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('url', 'username', 'email', 'groups')
class GroupSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Group
fields = ('url', 'name')
views.py:
class UserViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset = User.objects.all().order_by('-date_joined')
serializer_class = UserSerializer
class GroupViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows groups to be viewed or edited.
"""
queryset = Group.objects.all()
serializer_class = GroupSerializer
urls.py:
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)
router.register(r'rooms', views.RoomViewSet)
router.register(r'devices', views.DeviceViewSet)
router.register(r'deviceTypes', views.DeviceTypeViewSet)
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
现在一切正常,但我无法找到如何删除或更新用户或组,似乎我只能添加用户和组并查看它们。 所以我的问题是: 如何修改此代码以删除/更新用户和组?
答案 0 :(得分:3)
代码很好,您只需要分别使用PUT
和DELETE
数据方法进行更新和删除(而不是GET/POST
)
您可以在代码示例中看到docs
中的ModelViewSet
class SnippetViewSet(viewsets.ModelViewSet):
"""
This viewset automatically provides `list`, `create`, `retrieve`,
`update` and `destroy` actions.
的文档
ModelViewSet类提供的操作是
.list()
,.retrieve()
,.create()
,.update()
,.partial_update()
和.destroy()
。
答案 1 :(得分:1)
您必须使用方法DELETE执行HTTP请求,而不使用任何标题和正文。 示例
<!DOCTYPE html>
<html>
<header>
<h2 style="bottom: -130px">
<a href="#" class=h ead-link> A B E L</a>
</h2>
<div class="dropdown" style="float;right">
<button class="dropbtn">Menu</button>
<div class="dropdown-content" style="right;0">
<a href="#">About </a>
<a href="#">Portfolio</a>
<a href="#">Contact</a>
</div>
</div>
<div class="title"></div>
<title>A B E L </title>
</header>
<body>
<div id="hl-content" class="content"></div>
<p>
<span style="color:navajowhite">
<b>Hello, Welcome to my website.</b>
</span>
<BR></BR>
My name is Abel, a senior college student currently residing in the Bay Area. This portfolio will give you a good background about me including my specialties, expertise and even hobbies. As you can probably tell from the background pictures, I like traveling
and taking photos!
</p>
<p>This is my first ever website that I created using HTML and CSS, and it's a pleasure to finally be able to share it with you. I am happy to receive any feedback, recommendations or opportunities from you, so don't hesitate to contact me.</p>
<div class="footer">
<ul>
<li><a href="#" target="#"> About</a></li>
<li><a href="#" target="#">Portfolio</a></li>
<li> <a href="#" target="#"> Contact </a></li>
</ul>
</div>
</body>
</html>
仅对比,创建将执行下一个CURL(注意标题“Content-Type”):
curl -X DELETE http://127.0.0.1:8000/api/v1/persona/4/
干杯C:
答案 2 :(得分:0)
使用“ curl”或“ httpie”执行删除或更新或任何其他请求。
示例:
http DELETE example.org/todos/7
or
curl -X "DELETE" http://www.url.com/page
在此处查看详细信息: