gpt4 book ai didi

python - 500 错误 : null value in column "timeline_id" violates not-null constraint

转载 作者:太空宇宙 更新时间:2023-11-04 01:46:01 26 4
gpt4 key购买 nike

我目前是一名学生,正在尝试使用 python/django 和 React 创建一个全栈网络应用程序。在构建我的后端时,我遇到了一个问题,当发布一个包含关联的对象时,关联的 id 在响应有效负载和数据库之间丢失了。我知道状态正在更新,并且可以毫无问题地创建后端不使用关联的其他对象。我假设我的错误一定是在我制作的模型或序列化器中的某个地方,但到目前为止我还没有找到它。

500 错误信息:

Integrity error at "api/v1/event"
null value in column "timeline_id" violates not-null constraint
DETAIL: failing row contains (index_id, junk_data, junk_data, 1, null)

我的模型:

from django.db import models

class Timeline(models.Model):
name = models.CharField(max_length=50, default='n/a')

def __str__(self):
return self.name


class Event(models.Model):
name = models.CharField(max_length=25, default='n/a')
description = models.CharField(max_length=150, default='n/a')
coordinate = models.IntegerField(default=0)
timeline = models.ForeignKey(Timeline, on_delete=models.CASCADE, related_name="events")

def __str__(self):
return self.name


class Note(models.Model):
article = models.TextField(default='n/a')
event = models.ForeignKey(Event, on_delete=models.CASCADE, related_name='notes')
# event = models.ManyToManyField(Event)

def __str__(self):
return self.article

我的观点:

from rest_framework import viewsets

from .serializers import TimelineSerializer, EventSerializer, NoteSerializer
from .models import Timeline, Event, Note

class TimelineView(viewsets.ModelViewSet):
queryset = Timeline.objects.all()
serializer_class = TimelineSerializer

class EventView(viewsets.ModelViewSet):
queryset = Event.objects.all()
serializer_class = EventSerializer

class NoteView(viewsets.ModelViewSet):
queryset = Note.objects.all()
serializer_class = NoteSerializer

我的序列化器:

from rest_framework import serializers

from .models import Timeline, Note, Event

class NoteSerializer(serializers.ModelSerializer):

class Meta:
model = Note
fields = ('id', 'title', 'article')


class EventSerializer(serializers.ModelSerializer):
notes = NoteSerializer(many=True, read_only=True)

class Meta:
model = Event
fields = ('id', 'name', 'description', 'coordinate', 'notes')


class TimelineSerializer(serializers.ModelSerializer):
events = EventSerializer(many=True, read_only=True)

class Meta:
model = Timeline
fields = ('id', 'name', 'events')

链接到 github 仓库: https://github.com/dantehollo/world_builder_site

我在 linux (ubuntu 18.04.3) 上使用 python 3.6.8

我尽量做到具体和简短。如果有任何遗漏,请告诉我,我会在看到请求后立即发布。非常感谢任何有关这是什么、为什么会发生以及如何避免再次发生的帮助,谢谢。

最佳答案

根据您的Event 模型,timeline 属性(也是外键)是必需的,不能为空。而且您忘记将此 timeline 添加到 EventSerializer 的字段中:

class EventSerializer(serializers.ModelSerializer):
notes = NoteSerializer(many=True, read_only=True)

class Meta:
model = Event
fields = ('id', 'name', 'description', 'coordinate', 'notes', 'timeline')

现在,如果您不在请求中发送 timeline,DRF 将给出您必须包含它的 ValidationError。

关于python - 500 错误 : null value in column "timeline_id" violates not-null constraint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59077537/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com