简体   繁体   English

将 Swagger 与 Django Rest Framework 一起使用,我可以在不同的字段而不是一个主体中看到 POST 参数吗

[英]Using Swagger with Django Rest Framework, can I see POST parameters in different fields instead of one body

I'm actually create APIs on a Django Website using Django Rest Framework.我实际上是在使用 Django Rest Framework 在 Django 网站上创建 API。 I'm trying to document them using Swagger.我正在尝试使用 Swagger 记录它们。

I'm using Django 2.1, django-rest-swagger 2.2 and djangorestframework 3.11我正在使用 Django 2.1、django-rest-swagger 2.2 和 djangorestframework 3.11

Everything is nearly working as expected except something :除了某些事情之外,一切都几乎按预期工作:

Let me explain you :让我解释一下:

I have this model (models.py)我有这个模型(models.py)

class Technology(models.Model):
    """
    This model defines the different technologies
    """
    name = models.CharField(max_length=CHAR_SHORT)
    path = models.CharField(max_length=CHAR_SHORT, validators=[validate_tech_path], help_text='this is only used to construct the url')
    image = models.ImageField()
    mailer = models.EmailField(blank=True)
    external = models.BooleanField(default=False)
    internal = models.BooleanField(default=False)

    class Meta:
        verbose_name_plural = "technologies"
        ordering = ['name']

    def __str__(self):
        return self.name

Then I have the corresponding serializer class (serializer.py):然后我有相应的序列化器类(serializer.py):

class TechnologySerializer(serializers.ModelSerializer):
    """
    This model defines the different technologies
    """
    class Meta:
        model = Technology
        fields = ('id', 'name', 'path', 'image', 'mailer', 'external', 'internal')

Finally I have my view with generated APIs (views.py):最后,我对生成的 API (views.py) 有了我的看法:

class TechnologyViewSet(viewsets.ModelViewSet):

    queryset = Technology.objects.all()
    serializer_class = TechnologySerializer
    http_method_names = ['get','post','delete','put']

Here's the result :结果如下:

Api description 1 Api description 2接口说明 1 接口说明 2

As you see on the picture above, the parameters are une the json body.如上图所示,参数与 json 主体无关。

Is it possible to have something like this for all the parameters :是否可以为所有参数设置类似的内容:

API parameter wanted Thanks a lot.需要 API 参数 非常感谢。

Try using ListCreateAPIView instead of modelviewset you will be able to see your post https://www.django-rest-framework.org/api-guide/generic-views/#listcreateapiview .尝试使用ListCreateAPIView而不是modelviewset你将能够看到你的帖子https://www.django-rest-framework.org/api-guide/generic-views/#listcreateapiview

from rest_framework.generics import ListCreateAPIView
class TechnologyViewSet(ListCreateAPIView):

    queryset = Technology.objects.all()
    serializer_class = TechnologySerializer

Hope it help.希望有帮助。

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

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