Django DRF:如何使待办事项依赖于另一个项目

时间:2019-11-16 20:01:05

标签: django django-rest-framework

嗨,我正在根据一个教程构建一个待办事项应用程序,我想对其进行一些改进。我想要一个待办事项依赖于另一个。例如,如果todo1依赖于todo2,则不应允许我在完整的todo2之前检查todo1。

这是我的 models.py

from django.db import models
from datetime import datetime
from django.contrib.auth.models import User

class TodoList(models.Model):
    name = models.CharField(max_length=200, default="blank title")
    is_completed = models.BooleanField(default=False)
    completed_at = models.DateTimeField(default=datetime.now(), null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    owner = models.ForeignKey(User, on_delete=models.CASCADE, null=True)

    class Meta:
        verbose_name = "Todo List"
        verbose_name_plural = "Todo Lists"
        ordering = ["name","created_at", "is_completed", ]

    def __str__(self):
            return self.name


class TodoItem(models.Model):
    name = models.CharField(max_length=200, default="blank title")
    todo_list = models.ForeignKey(TodoList, on_delete=models.CASCADE, null=False)
    description = models.TextField(max_length=200, default="Blank text")
    is_completed = models.BooleanField(default=False)
    completed_at = models.DateTimeField(default=datetime.now(), null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    deadline = models.DateTimeField(default=datetime.now(), null=True, blank=True)
    owner = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    previous_item = models.IntegerField(default=0)


    class Meta:
        verbose_name = "Todo Item"
        verbose_name_plural = "Todo Item"
        ordering = ["id","created_at", "is_completed","deadline","name" ]

    def __str__(self):
            return self.name

Views.py

from django.shortcuts import render
from rest_framework.views import APIView
from django.http import HttpResponseRedirect
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from rest_framework import viewsets, permissions, status
from rest_framework.decorators import action, api_view
from rest_framework.response import Response
from .serializers import *
from .models import *





class TodoItemView(viewsets.ModelViewSet):
    serializer_class = TodoItemSerializer


    def  get_queryset(self):
         queryset = TodoItem.objects.filter(owner=self.request.user)
         return queryset

    @action(detail=False)
    def show_completed(self, request, pk=None):
        completed_tasks = TodoItem.objects.filter(owner=self.request.user, is_completed=False)
        serializer = TodoItemSerializer(completed_tasks, many=True)
        data= serializer.data
        return Response(data)

class TodoListView(viewsets.ModelViewSet):
    serializer_class = TodoListSerializer
    queryset = TodoList.objects.all()

    def  get_queryset(self):
         queryset = TodoItem.objects.filter(owner=self.request.user)
         return queryset


def SignupView(request):
    if request.method == 'POST':
        f = UserCreationForm(request.POST)
        if f.is_valid():
            f.save()
            return redirect('register')

    else:
        f = UserCreationForm()

    return render(request, 'signup.html', {'form': f})


@api_view(['GET'])
def current_user(request):
    """
    Determine the current user by their token, and return their data
    """

    serializer = UserSerializer(request.user)
    return Response(serializer.data)


class UserList(APIView):
    """
    Create a new user. It's called 'UserList' because normally we'd have a get
    method here too, for retrieving a list of all User objects.
    """

    permission_classes = (permissions.AllowAny)

    def post(self, request, format=None):
        serializer = UserSerializerWithToken(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

我还在views.py中使用了APIView和ModelViewSet。看起来不错,还是应该每次都使用相同的方法?如果是这样,有人可以帮助我将SignupView编写为ModelViewSet吗?

0 个答案:

没有答案