Python 非空约束失败:代码段\u choice.post\u id

Python 非空约束失败:代码段\u choice.post\u id,python,django,django-rest-framework,django-orm,Python,Django,Django Rest Framework,Django Orm,我试图在这里使用POST方法,但它给我带来了一个错误 这是Models.py: from django.db import models class Post(models.Model): post_text=models.CharField(max_length=200) def __str__(self): return self.post_text class Choice(models.Model): post=models.ForeignK

我试图在这里使用POST方法,但它给我带来了一个错误

这是Models.py:

from django.db import models

class Post(models.Model):
    post_text=models.CharField(max_length=200)

    def __str__(self):
        return self.post_text

class Choice(models.Model):
    post=models.ForeignKey(Post, on_delete=models.CASCADE)
    choice_text=models.CharField(max_length=200)
    likes=models.IntegerField(default=0)

    def __str__(self):
        return self.choice_text
这是serializes.py:

from rest_framework import serializers
from snippets.models import Post, Choice

class PostSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model=Post
        fields=['id','post_text']

class ChoiceSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model=Choice
        fields=['id','choice_text','likes']
这是views.py:


from django.shortcuts import render
from rest_framework import generics
from rest_framework.reverse import reverse
from .models import Choice, Post
from django.http import Http404
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from snippets.serializers import ChoiceSerializer, PostSerializer

class PostList(generics.ListCreateAPIView):
    queryset=Post.objects.all()
    serializer_class=PostSerializer
    name='post-list'

class PostDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset=Post.objects.all()
    serializer_class=PostSerializer
    name='post-detail'

class ChoiceList(generics.ListCreateAPIView):
    queryset=Choice.objects.all()
    serializer_class=ChoiceSerializer
    name='choice-list'

class ChoiceDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset=Choice.objects.all()
    serializer_class=ChoiceSerializer
    name='choice-detail'

class ApiRoot(generics.GenericAPIView):
    name='api-root'

    def get(self, request, *args, **kwargs):
        return Response({
        'posts': reverse(PostList.name, request=request),
        'choices': reverse(ChoiceList.name, request=request),
        }) 
URL.py:

urlpatterns=[
    path('post-list/', views.PostList.as_view(), name='post-list'),
    path('post-list/<int:pk>/', views.PostDetail.as_view()),
    path('choice-list/', views.ChoiceList.as_view(), name='choice-list'),
    path('choice-list/<int:pk>/', views.ChoiceDetail.as_view()),
    path('',views.ApiRoot.as_view(),name=views.ApiRoot.name),
]
我得到了这个错误:

IntegrityError at /choice-list/
NOT NULL constraint failed: snippets_choice.post_id
如果我提供id,为什么会抛出错误


即使在Postman中,它也要求我输入“choice_text”,即使我提供了choice_text。

您正在尝试创建一个选项,并且该选项模型与具有not null约束的Post模型相关,因此可以将Post foreignkey设置为null

post=models.ForeignKey(post,on\u delete=models.CASCADE)

post=models.ForeignKey(post,on_delete=models.CASCADE,null=True)

或者,您必须首先创建一篇文章,然后在创建选项时传递文章id

发送请求到/发送列表/

{
   "post_text": "Hello World!"
}
它将创建一个post id为post_id=1的post

将请求发布到/选择列表/

{ 
    "choice_text": "random text",
    "likes": 0,
    "post": 1
}
{ 
    "choice_text": "random text",
    "likes": 0,
    "post": 1
}