[英]JSON data not being sent - Django Rest Framework / React
So, I'm new to Django and after hours of searching / trying different things I can't figure this out. 因此,我是Django的新手,经过数小时的搜索/尝试了其他事情,我无法弄清这一点。
I have a form that submits my components state to the api, adds the values to the database, and then displays them in a table. 我有一个表单,将组件状态提交给api,将值添加到数据库,然后在表中显示它们。 Everything is getting into the database except for the "projects" field. 除“项目”字段外,所有内容都进入数据库。 When I look in the React Dev tools, the value I expect is in my state. 当我查看React Dev工具时,期望的值处于我的状态。
project: 3
But after I submit the form the returned value shows as null. 但是在我提交表单后,返回值显示为null。
project: null
I'm really not sure why the value is null. 我真的不确定为什么该值为null。
Here is my models.py 这是我的models.py
class Completed(models.Model):
completed = models.BooleanField(default=False)
url = models.CharField(max_length=100)
handle = models.CharField(max_length=30)
filename = models.CharField(max_length=100)
size = models.IntegerField()
source = models.CharField(max_length=50)
uploadId = models.CharField(max_length=50)
originalPath = models.CharField(max_length=50)
owner = models.ForeignKey(
User, related_name="completed", on_delete=models.CASCADE, null=True)
project = models.ForeignKey(
Project, related_name="projectId", on_delete=models.CASCADE, null=True)
uploadDate = models.DateTimeField(auto_now_add=True)
Here is the Project model 这是项目模型
class Project(models.Model):
projectCode = models.CharField(max_length=10)
projectName = models.CharField(max_length=100)
user = models.ForeignKey(
User, related_name="projects", on_delete=models.CASCADE, null=True)
editor = models.ForeignKey(
User, on_delete=models.CASCADE, null=True)
creationDate = models.DateTimeField(auto_now_add=True)
completedDate = models.DateTimeField(null=True, blank=True)
dueDate = models.DateTimeField(null=True, blank=True)
def __str__(self):
return f"{self.projectName}"
Here are the serializers 这是序列化器
# Project Serializer
class ProjectSerializer(serializers.ModelSerializer):
class Meta:
model = Project
fields = [
'id',
'projectCode',
'projectName',
'user'
]
depth = 1
# Completed Serializer
class CompletedSerializer(serializers.ModelSerializer):
# project = ProjectSerializer()
class Meta:
model = Completed
fields = [
'completed',
'url',
'handle',
'filename',
'size',
'source',
'uploadId',
'originalPath',
'owner',
'project',
'uploadDate'
]
depth = 1
I tried adding the ProjectSerializer into the CompletedSerializer but it gave me a 400 bad-request error. 我尝试将ProjectSerializer添加到CompletedSerializer中,但这给了我400错误请求错误。
And here is the viewset 这是视图集
class CompletedViewSet(viewsets.ModelViewSet):
permission_classes = [
permissions.IsAuthenticated
]
serializer_class = CompletedSerializer
def get_queryset(self):
queryset = self.request.user.completed.all().filter(completed=True)
return queryset
def perform_create(self, serializer):
serializer.save(owner=self.request.user)
Can anyone tell me what I'm doing wrong here? 谁能告诉我我在做什么错?
You're using depth=1
on your serializers. 您在序列化器上使用depth=1
。 That means you'll have nested serializer for your ForeignKey
. 这意味着您将为ForeignKey
嵌套了序列化程序。 By default, nested serializers are read-only. 默认情况下,嵌套序列化器是只读的。 If you want a writable nested serializer, you have to uncomment your # project = ProjectSerializer()
on CompletedSerializer
and pass all the data for the creation of a project instance. 如果需要可写的嵌套序列化程序,则必须取消注释CompletedSerializer
上的# project = ProjectSerializer()
并传递所有数据以创建项目实例。 I don't think this is what you want. 我认为这不是您想要的。 If I've understood well, you only want to reference an existing project. 如果我了解得很好,那么您只想引用一个现有项目。 So, the best way to accomplish that, in my opinion, is removing the depth
on your CompletedSerializer
. 因此,我认为实现此目标的最佳方法是消除CompletedSerializer
的depth
。 If you need the project when you list or retrieve a Completed instance, go with a different serializer based on the request action. 如果您在列出或检索Completed实例时需要项目,请根据请求操作使用其他序列化程序。 Here more details about writable-nested serializers https://www.django-rest-framework.org/community/3.0-announcement/#writable-nested-serialization . 此处提供了有关可写嵌套序列化程序的更多详细信息, https://www.django-rest-framework.org/community/3.0-announcement/#writable-nested-serialization 。
For using different serializers, this is a reference. 对于使用不同的序列化器,这是参考。 Add this code in your ViewSet: 将此代码添加到您的ViewSet中:
def get_serializer_class(self):
if self.action == 'list' or self.action == 'retrieve':
return CompletedNestedSerializer
return CompletedSerializer
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.