rest_framework NOT NULL 约束失败:tasks_tasks.assigner_id

rest_framework NOT NULL constraint failed: tasks_tasks.assigner_id

我正在制作一个应用程序,分配者可以将任务分配给受让人 当我 运行 服务器并尝试添加任务时,我收到此错误:

NOT NULL 约束失败:tasks_tasks.assigner_id

这是我的观点:

from rest_framework import viewsets, permissions
from .models import Tasks
from .serializers import TasksSerializer, UserSerializer
from django.contrib.auth.models import User


class UserViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer


class TasksViewSet(viewsets.ModelViewSet):
    queryset = Tasks.objects.all()
    serializer_class = TasksSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)

    def prefrom_create(self, serializer):
        serializer.save(self, assigner=self.request.user)

这是我的 serializers.py

from rest_framework import serializers
from .models import Tasks
from django.contrib.auth.models import User


class TasksSerializer(serializers.ModelSerializer):
    assigner = serializers.ReadOnlyField(source='assigner.username')

    class Meta:
        model = Tasks
        fields = ('id', 'url', 'title', 'description', 'assigner', 'assignee')


class UserSerializer(serializers.ModelSerializer):
    tasks = serializers.HyperlinkedIdentityField(many=True, view_name='tasks_details', read_only=True)

    class Meta:
        model = User
        fields = ('id', 'url', 'username', 'tasks')

最后是我的 models.py:

from django.db import models

class Tasks(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    title = models.CharField(max_length=100)
    description = models.TextField()
    assigner = models.ForeignKey('auth.User', related_name='tasks', on_delete=models.CASCADE)
    assignee = models.ForeignKey('auth.User', related_name='assigned', on_delete=models.CASCADE)

    class Meta:
        ordering = ('created',)

您已将 assigner 设为只读字段,来源为 assigner.username - 这是您的问题。我认为您可以通过定义两个字段来解决这个问题,一个 read_only 和一个 write_only:

class TasksSerializer(serializers.ModelSerializer):
assigner_name = serializers.ReadOnlyField(source='assigner.username')
assigner = serializers.PrimaryKeyRelatedField(write_only=True, queryset=User.objects.all(), required=True)
class Meta:
    model = Tasks
    fields = ('id', 'url', 'title', 'description', 'assigner', 'assignee', 'assigner_name')

这段代码的效果是,当你的序列化器是'writing'(反序列化)时,它将接受你传递给保存方法的分配器ID作为关键字参数assigner。当它随后将任务序列化为 json 时,它将在键 assigner_name 下查找 assigner.name 和 return 并错过 assigner.

在视图中,然后将主键(而不是整个对象)传递给序列化程序保存方法:

serializer.save(assigner=self.request.user.pk)