简体   繁体   English

drf_yasg @swagger_auto_schema 未显示 POST 请求所需的参数

[英]drf_yasg @swagger_auto_schema not showing the required parameters for POST Request

I am using django-yasg to create an api documentation.我正在使用 django-yasg 创建一个 api 文档。 But no parameters are showing in the documentation to create post request.但是文档中没有显示用于创建发布请求的参数。 Following are my codes: After that in swagger api, no parameters are showing for post request to create the event以下是我的代码: 在 swagger api 之后,没有显示用于创建事件的发布请求的参数在此处输入图像描述

model.py model.py

class Events(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    category = models.ForeignKey(EventCategory, on_delete=models.CASCADE)
    name = models.CharField(max_length=250, null=True)
    posted_at = models.DateTimeField(null=True, auto_now=True)
    location = models.CharField(max_length=100, null=True)
    banner = models.ImageField(default='avatar.jpg', upload_to='Banner_Images')
    start_date = models.DateField(
        auto_now=False, auto_now_add=False, blank=True, null=True)
    end_date = models.DateField(
        auto_now=False, auto_now_add=False, blank=True, null=True)
    description = models.TextField(max_length=2000, null=True)
    completed = models.BooleanField(default=False)

    def __str__(self):
        return f'{self.name}'

    class Meta:
        verbose_name_plural = "Events"

serializers.py序列化器.py

class EventSerializer(serializers.ModelSerializer):
    user = UserSerializer(read_only=True, many=False)
    category = EventCategorySerializer(read_only=True, many=False)

    class Meta:
        model = Events
        fields = '__all__'

views.py视图.py

@api_view(['POST'])
@permission_classes([IsAuthenticated])
@user_is_organization
@swagger_auto_schema(
    request_body=EventSerializer
)
def registerEvent(request):
    """
        To Register an events, you must be an organization
    """
    data = request.data
    print("==================")
    print(data)
    print("==================")
    try:
        Event = Events.objects.create(
            user = request.user,
            category=EventCategory.objects.get(category=data['category']),
            name=data['name'],
            location=data['location'],
            start_date=data['start_date'],
            end_date=data['end_date'],
            description=data['description'],
            completed=data['completed'],
        )
        serializer = EventSerializer(Event, many=False)
        Event = Events.objects.get(id=serializer.data['id'])
        Event.banner = request.FILES.get('banner')
        Event.save()
        serializer = EventSerializer(Event, many=False)
        return Response(serializer.data)
    except:
        message = {'detail': 'Event with this content already exists'}
        return Response(message, status=status.HTTP_400_BAD_REQUEST)

I got it running with following changes in views.py我在 views.py 中进行了以下更改使其运行

@swagger_auto_schema(
    methods=['post'],
    request_body=openapi.Schema(
        type=openapi.TYPE_OBJECT,
        required=['category','name', 'location', 'start_date', 'end_date', 'description', 'completed', 'banner'],
        properties={
            'category':openapi.Schema(type=openapi.TYPE_STRING),
            'name':openapi.Schema(type=openapi.TYPE_STRING),
            'location':openapi.Schema(type=openapi.TYPE_STRING),
            'start_date':openapi.Schema(type=openapi.TYPE_STRING, default="yyyy-mm-dd"),
            'end_date':openapi.Schema(type=openapi.TYPE_STRING, default='yyyy-mm-dd'),
            'description':openapi.Schema(type=openapi.TYPE_STRING), 
            'completed':openapi.Schema(type=openapi.TYPE_BOOLEAN, default=False),
            'banner': openapi.Schema(type=openapi.TYPE_FILE),
        },
    ),
    operation_description='Create an events' 
)
@api_view(['POST'])
@permission_classes([IsAuthenticated])
@user_is_organization
def registerEvent(request):
    """
        To Register an events, you must be an organization
    """
    data = request.data
    print("==================")
    print(data)
    print(type(data))
    category=EventCategory.objects.get(category=data['category']),
    print(category)
    print(type(data["start_date"]))
    print("==================")
    try:
        Event = Events.objects.create(
            user = request.user,
            category=EventCategory.objects.get(category=data['category']),
            name=data['name'],
            location=data['location'],
            start_date=data['start_date'],
            end_date=data['end_date'],
            description=data['description'],
            completed=data['completed'],
        )
        print("****************************")
        serializer = EventSerializer(Event, many=False)
        Event = Events.objects.get(id=serializer.data['id'])
        Event.banner = request.FILES.get('banner')
        Event.save()
        serializer = EventSerializer(Event, many=False)
        return Response(serializer.data)
    except ValidationError as e:
        return Response({"ValidationError" : e}, status = status.HTTP_400_BAD_REQUEST)
    except Exception as e:
        message = {'error': e}
        return Response(message, status=status.HTTP_400_BAD_REQUEST)

在此处输入图像描述

can you modify your swagger_auto_schema decorator like this?你能像这样修改你的swagger_auto_schema装饰器吗?

@swagger_auto_schema(
    methods=['post',],
    request_body=EventSerializer )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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