Home > Enterprise >  Django Swagger won't allow me to use POST method (no parameters shown)
Django Swagger won't allow me to use POST method (no parameters shown)

Time:11-28

I'm using djangorestframework together with drf-spectacular modules for a Django project, and I'm trying to build some basic API methods for my Project model. Its structure looks like this:

from django.db import models

# Create your models here.

class Project(models.Model):
    title = models.CharField(max_length = 128)
    description = models.TextField()
    image = models.URLField()
    date = models.DateTimeField(auto_now_add=True)

I also have a serializer for the model, looking like this:

from rest_framework import serializers
from api.models.Project import Project


class ProjectSerializer(serializers.ModelSerializer):
    class Meta:
        model = Project
        fields = ['title', 'description', 'image', 'date']

Then, in views.py, I created two functions: project_list_view, which either lets you to GET all the Project objects from the database, or lets you POST a new object. And finally, project_detail_view, which lets you GET a Project object by typing in its pk (integer id). These are my two functions:

@api_view(['GET', 'POST'])
def project_list_view(request):
    if request.method == 'GET':
        projects = Project.objects.all()
        serializer = ProjectSerializer(projects, many=True)
        return Response(serializer.data)

    elif request.method == "POST":
        serializer = ProjectSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

@api_view(['GET'])
def project_detail_view(request, pk):
    if request.method == "GET":
        try:
            project = Project.objects.get(pk = pk)
            serializer = ProjectSerializer(project, many = False)
            return Response(serializer.data, status = status.HTTP_200_OK)
        except:
            return Response(status=status.HTTP_404_NOT_FOUND)

The GET from project_list_view and project_detail_view work, but my problem lays in the POST method. My Swagger is set to display its API Schema when accessing enter image description here

My question is: Why won't Swagger display fields for each parameter of the model? Thank you.

CodePudding user response:

Swagger Grabs the fields from a serializer_class variable. I really recommend you change the format to Class-Based Views. Something using mixins or generic class.

Your view could be like

class ProjectView(mixins.RetrieveModelMixin, mixins.CreateModelMixin, viewsets.GenericViewSet):

    permission_classes = [permissions.IsAuthenticated, ] 
    serializer_class = ProjectSerializer
    queryset = Project.objects.all()

    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        return self.create(request, *args, **kwargs)

More on Mixins and Generic Views

  • Related