Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Swagger Docs to Django Without Django REST FRAMEWORK

Tags:

I am working on a Django project and there's requirement to build the APIs without using the Django rest-framework library.

I'm having a hard time figuring out a work around as most available libraries are already tied to the Django rest-framework pip library.

Can someone point me to an example, library or resource for self documented APIs using swagger docs that

supports at least Python 3.7

like image 911
Adebayo A. Daniel Avatar asked Jan 06 '21 18:01

Adebayo A. Daniel


People also ask

Do you need Django Rest Framework?

Django REST framework (DRF) is a powerful and flexible toolkit for building Web APIs. Its main benefit is that it makes serialization much easier. Django REST framework is based on Django's class-based views, so it's an excellent option if you're familiar with Django.

How does Django integrate swagger UI?

For a quick start, use shortcut method get_swagger_view which will generate Swagger UI documentation for your API. This method have two optional parameters title (of your Swagger documentation) and url (to your Swagger documentation).

Is Django Rest Framework the same as Django?

Django is the web development framework in python whereas the Django Rest Framework is the library used in Django to build Rest APIs. Django Rest Framework is especially designed to make the CRUD operations easier to design in Django. Django Rest Framework makes it easy to use your Django Server as an REST API.


1 Answers

You can generate API Docs using APISpec (https://pypi.org/project/apispec/)

APISpec supports OpenAPI and marshmallow and can be implemented in Plain python and is not tied to Django Framework

You can specify the content of the doc using Doc String in the following way,

@app.route("/random")
def random_pet():
    """A cute furry animal endpoint.
    ---
    get:
      description: Get a random pet
      security:
        - ApiKeyAuth: []
      responses:
        200:
          content:
            application/json:
              schema: PetSchema
    """
    pet = get_random_pet()
    return PetSchema().dump(pet)

Docs: https://apispec.readthedocs.io/en/latest/

There are several ways to build api's without using Django REST Framework, some of alternatives include:

FAST API - https://fastapi.tiangolo.com/

Flask - https://flask.palletsprojects.com/en/2.0.x/

Cherrypy - https://docs.cherrypy.dev/en/latest/

like image 111
код е Avatar answered Sep 30 '22 18:09

код е