我的 Django 后端是这样构建的:我们有学习计划,其中有与之相关的任务。现在,我通过如下所示的网址获取学习计划:"url:8000/database/studyplans"。
我想要的是将学习计划连接到您登录的用户。所以网址看起来像这样:"url:8000/database/studyplans/WilliamABC",或者其他用户:"url:8000/database/studyplans/Bruce123">
模型:
from django.db import models
from datetime import date
#Create your models here.
class StudyplanTask(models.Model):
title = models.CharField(max_length=150)
description = models.CharField(max_length=1000, blank=True)
state = models.CharField(max_length=20)
doneby = models.DateField(default=date.today)
class Meta:
verbose_name_plural = 'Tasks'
def __str__(self):
return self.title
class StudiePlan(models.Model):
title = models.CharField(max_length=150)
description = models.CharField(max_length=1000)
tasks = models.ManyToManyField(StudyplanTask, blank=True)
doneby = models.DateField(default=date.today)
class Meta:
verbose_name_plural = 'Studyplans'
def __str__(self):
return self.title
序列化程序:
from rest_framework import serializers
from .models import StudiePlan, StudyplanTask
class StudyplanTaskSerializer(serializers.ModelSerializer):
class Meta:
model = StudyplanTask
fields = ('id', 'title', 'description', 'state', 'doneby')
class StudyplanTaskNameSerializer(serializers.ModelSerializer):
class Meta:
model = StudyplanTask
fields = ('id', 'title', 'description', 'state', 'doneby')
class StudyplanSerializer(serializers.ModelSerializer):
tasks = StudyplanTaskNameSerializer(many=True, read_only=True)
class Meta:
model = StudiePlan
fields = ('id', 'title', 'description', 'tasks', 'doneby')
视图:
from django.shortcuts import render
from rest_framework import viewsets, permissions
from .models import StudiePlan, StudyplanTask
from .serializers import StudyplanSerializer, StudyplanTaskSerializer
# Create your views here.
class StudyplanView(viewsets.ModelViewSet):
http_method_names = ['get', 'post', 'put', 'delete', 'patch']
queryset = StudiePlan.objects.all()
serializer_class = StudyplanSerializer
#get
def get(self, request):
hello_param = request.GET["helloParam"]
#post
def post(self, request):
hello_param = request.POST["helloParam"]
#put
def put(self, request):
hello_param = request.PUT["helloParam"]
#patch
def patch(self, request):
hello_param = request.PATCH["helloParam"]
#delete
def delete(self, request):
hello_param = request.DELETE["helloParam"]
class StudyplanTaskView(viewsets.ModelViewSet):
http_method_names = ['get', 'post', 'put', 'delete', 'patch']
queryset = StudyplanTask.objects.all()
serializer_class = StudyplanTaskSerializer
#get
def get(self, request):
hello_param = request.GET["helloParam"]
#post
def post(self, request):
hello_param = request.POST["helloParam"]
#put
def put(self, request):
hello_param = request.PUT["helloParam"]
#patch
def patch(self, request):
hello_param = request.PATCH["helloParam"]
#delete
def delete(self, request):
hello_param = request.DELETE["helloParam"]
网址:
from django.urls import path, include
from . import views
from rest_framework import routers
router = routers.DefaultRouter()
router.register('studyplans/', views.StudyplanView)
router.register('studyplan-tasks/', views.StudyplanTaskView)
urlpatterns = [
path('', include(router.urls)),
]
应用程序接口:
[
{
"id": 1,
"title": "Learn to Build a Mobile Frontend",
"description": "en opresesntion som är bra dokumenterad en opresesntion som är bra dokumenterad en opresesntion som är bra dokumenterad en opresesntion som är bra dokumenterad",
"tasks": [
{
"id": 1,
"title": "Learn Python",
"description": "en opresesntion som är bra dokumenterad en opresesntion som är bra dokumenterad en opresesntion som är bra dokumenterad en opresesntion som är bra dokumenterad",
"state": "active",
"doneby": "2019-05-31"
}
],
"doneby": "2019-05-31"
}
]
首先,您需要将StudiePlan链接到用户
class StudiePlan(models.Model):
title = models.CharField(max_length=150)
description = models.CharField(max_length=1000)
tasks = models.ManyToManyField(StudyplanTask, blank=True)
doneby = models.DateField(default=date.today)
user = models.ForeginKey(User, on_delete=models.CASCADE)
然后创建视图
class UserStudyplanView(StudyplanView):
def get_queryset(self):
return self.request.user.studieplan.all()
和一个网址
router.register('user-studyplan-tasks/', views.UserStudieplanView)