Adding Swagger Docs to Django Without Django REST FRAMEWORK

Viewed 721

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

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/

Related