[英]Django : null value in column "id" of relation violates not-null constraint
I want to post data into my model in raw format.我想以原始格式将数据发布到我的 model 中。 but every time I post data and pass the foreign key it returns null.但每次我发布数据并传递外键时,它都会返回 null。 I don't know why I tried everything.我不知道为什么我尝试了一切。 I passed the column as series: 2
and series_id:2
nothing's working.我将列作为series: 2
和series_id:2
没有任何工作。 i don't know what i'm doing wrong.我不知道我做错了什么。
This is my ViewSet这是我的视图集
class SeasonViewSet(viewsets.ModelViewSet):
queryset = Season.objects.all()
permission_classes = [
permissions.AllowAny
]
serializer_class = SeasonSerializer
Serializers.py序列化程序.py
class SeasonSerializer(serializers.ModelSerializer):
series = StringRelatedField()
class Meta:
model = Season
fields = '__all__'
Here's my model:这是我的 model:
class Season(models.Model):
name = models.CharField(max_length=100, blank=True)
number = models.IntegerField()
series = models.ForeignKey(Series, related_name="seasons", on_delete=models.CASCADE)
artwork_vr = models.ImageField(upload_to="images/tv", blank=True, default="default/default_video.png")
premiereDate = models.DateField(blank=True)
endDate = models.DateField(blank=True)
This is how I'm posting data to API这就是我将数据发布到 API 的方式
{
"series": 2,
"name": "Attack",
"number": 2,
"premiereDate": "2021-07-03",
"endDate": "2021-07-03"
}
and I get this error: null value in column "series_id" of relation "tvshow_season" violates not-null constraint DETAIL: Failing row contains (38, null, 4, default/default_video.png, 2021-07-03, , 2021-07-03).
我得到这个错误: null value in column "series_id" of relation "tvshow_season" violates not-null constraint DETAIL: Failing row contains (38, null, 4, default/default_video.png, 2021-07-03, , 2021-07-03).
change series = StringRelatedField()
to series = PrimaryKeyRelatedField(queryset=Series.objects.all())
So you could post your data API that way.将series = StringRelatedField()
更改为 series = PrimaryKeyRelatedField(queryset=Series.objects.all())
这样您就可以通过这种方式发布您的数据 API。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.