简体   繁体   中英

I can not send a post to django rest framework

I created a serializer to post a results, but when I try to use postman it says that the value 'enrollment_id' is null:

views.py

from rest_framework import generics

from .serializers import ResponseSerializer


class ResponseCreate(generics.CreateAPIView):
    serializer_class = ResponseSerializer

serializers.py

class ResponseSerializer(serializers.ModelSerializer):

    class Meta:
        model = Response
        fields = (
            'enrollment_id',
            'evaluation_id',
            'question_id',
            'question_component_id',
            'user_id',
        )

urls.py

from django.urls import path

from . import views

urlpatterns = [
    path('', views.ResponseCreate.as_view()),
]

In the postman post body I send the following json:

{
    "enrollment_id": 1,
    "user_id": 2,
    "question_component_id": 2,
    "question_id": 1,
    "evaluation_id": 1
}

Error postman:

IntegrityError at /response/
null value in column "enrollment_id" violates not-null constraint
DETAIL:  Failing row contains (9, null, null, null, null, null).

view

在此处输入图片说明

Edit:

Model Response:

class Response(models.Model):
    user = models.ForeignKey(
        User, 
        on_delete=models.CASCADE, 
        )
    enrollment = models.ForeignKey(
        Enrollment, 
        on_delete=models.CASCADE, 
        )
    evaluation = models.ForeignKey(
        Evaluation,
        on_delete=models.CASCADE, 
        )
    question = models.ForeignKey(
        Question,
        on_delete=models.CASCADE, 
        )
    question_component = models.ForeignKey(
        Question_Component,
        on_delete=models.CASCADE, 
        )

You have to indicate in your url patter the methods that are going to be allowed:

urlpatterns = [
    path('', views.ResponseCreate.as_view({'get': 'list'})),
]

I can see in the error you get that `GET method is not allowed, that's because you didn't indicate Django to allow it.

Try this view:

from rest_framework import viewsets
from .serializers import ResponseSerializer

class ResponseCreate(viewsets.ModelViewSet):
    queryset = models.Response.objects.all()
    serializer_class = serializers.ResponseSerializer

ModelViewSet has already the proper response for al methods, but you have to indicate in your url pattern which ones are allowed.

Edit: Your serializer need not add '_id' after the Model fields. It should be like this:

class ResponseSerializer(serializers.ModelSerializer):

    class Meta:
        model = Response
        fields = (
            'enrollment',
            'evaluation',
            'question',
            'question_component',
            'user',
        )

Now try seding the modified JSON via Postmen:

{
  "enrollment": 1,
  "user": 2,
  "question_component": 2,
  "question": 1,
  "evaluation": 1
}

You need to define a queryset in your view:

class ResponseCreate(generics.CreateAPIView):
    queryset = Response.objects.all()
    serializer_class = ResponseSerializer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM