简体   繁体   English

Django:关系的“id”列中的 null 值违反非空约束

[英]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: 2series_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.

相关问题 Django IntegrityError:关系“HomeFeed_interest”的“interestreceiver_id”列中的 null 值违反了非空约束 - Django IntegrityError: null value in column “interestreceiver_id” of relation “HomeFeed_interest” violates not-null constraint django.db.utils.IntegrityError:关系“shop_book”的“genre_id”列中的空值违反了非空约束 - django.db.utils.IntegrityError: null value in column "genre_id" of relation "shop_book" violates not-null constraint django.db.utils.IntegrityError:关系“university_university”的“auto_id”列中的 null 值违反非空约束 - django.db.utils.IntegrityError: null value in column "auto_id" of relation "university_university" violates not-null constraint Django - 列中的空值违反了 Django 管理中的非空约束 - Django - null value in column violates not-null constraint in Django Admin 关系“blog_comment”的“comment_id”列中的 null 值违反了非空约束 - null value in column “comment_id” of relation “blog_comment” violates not-null constraint “ user_id”列中的空值违反了非空约束 - null value in column “user_id” violates not-null constraint “ city_id”列中的空值违反了非空约束 - null value in column “city_id” violates not-null constraint Flask SQLAlchemy null “id”列中的值违反非空约束 - Flask SQLAlchemy null value in column "id" violates not-null constraint “ job_id”列中的空值违反了非空约束 - null value in column “job_id” violates not-null constraint Django:“is_admin”列中的 null 值违反了非空约束 - Django: null value in column "is_admin" violates not-null constraint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM