用djang rest framework写一个增删改查案例
时间: 2024-04-29 14:21:17 浏览: 30
假设我们要创建一个博客系统,包含文章和评论两个模型。我们可以使用Django Rest Framework来创建增删改查API。
首先,我们需要安装Django Rest Framework:
```python
pip install djangorestframework
```
接下来,我们需要创建两个模型类,一个是文章,一个是评论:
```python
from django.db import models
class Article(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
class Comment(models.Model):
article = models.ForeignKey(Article, on_delete=models.CASCADE)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
```
接下来,我们需要创建序列化器来序列化和反序列化模型实例:
```python
from rest_framework import serializers
from .models import Article, Comment
class ArticleSerializer(serializers.ModelSerializer):
class Meta:
model = Article
fields = '__all__'
class CommentSerializer(serializers.ModelSerializer):
class Meta:
model = Comment
fields = '__all__'
```
现在,我们可以创建视图来处理请求:
```python
from rest_framework import generics
from .models import Article, Comment
from .serializers import ArticleSerializer, CommentSerializer
class ArticleList(generics.ListCreateAPIView):
queryset = Article.objects.all()
serializer_class = ArticleSerializer
class ArticleDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Article.objects.all()
serializer_class = ArticleSerializer
class CommentList(generics.ListCreateAPIView):
queryset = Comment.objects.all()
serializer_class = CommentSerializer
class CommentDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Comment.objects.all()
serializer_class = CommentSerializer
```
最后,我们需要将视图映射到URL:
```python
from django.urls import path
from .views import ArticleList, ArticleDetail, CommentList, CommentDetail
urlpatterns = [
path('articles/', ArticleList.as_view(), name='article-list'),
path('articles/<int:pk>/', ArticleDetail.as_view(), name='article-detail'),
path('comments/', CommentList.as_view(), name='comment-list'),
path('comments/<int:pk>/', CommentDetail.as_view(), name='comment-detail'),
]
```
现在我们可以使用以下命令启动开发服务器:
```python
python manage.py runserver
```
我们可以使用curl或Postman等工具测试API:
```bash
# 获取所有文章
curl http://localhost:8000/articles/
# 创建一篇文章
curl -X POST -H "Content-Type: application/json" -d '{"title": "Hello world", "content": "This is my first post."}' http://localhost:8000/articles/
# 获取一篇文章
curl http://localhost:8000/articles/1/
# 更新一篇文章
curl -X PUT -H "Content-Type: application/json" -d '{"title": "Hello world", "content": "This is my updated post."}' http://localhost:8000/articles/1/
# 删除一篇文章
curl -X DELETE http://localhost:8000/articles/1/
# 获取所有评论
curl http://localhost:8000/comments/
# 创建一条评论
curl -X POST -H "Content-Type: application/json" -d '{"article": 1, "content": "Great post!"}' http://localhost:8000/comments/
# 获取一条评论
curl http://localhost:8000/comments/1/
# 更新一条评论
curl -X PUT -H "Content-Type: application/json" -d '{"article": 1, "content": "Awesome post!"}' http://localhost:8000/comments/1/
# 删除一条评论
curl -X DELETE http://localhost:8000/comments/1/
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)