我正在制作一个应用程序,分配者可以将任务分配给受让人 当我运行服务器并尝试添加任务时,我收到此错误:
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',)
答案 0 :(得分:1)
您已将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')
此代码的作用是当序列化程序“正在写入”(反序列化)时,它将接受作为关键字参数assigner
传递给save方法的分配器ID。然后,当它将任务序列化为json时,它会查找assigner.name
并将其返回到密钥assigner_name
下并遗漏assigner
。
在视图中,您将主键(而不是整个对象)传递给序列化程序保存方法:
serializer.save(assigner=self.request.user.pk)