django generate openapi schema command line

To generate an OpenAPI schema in Django using the command line, you can follow these steps:

  1. Install the necessary packages: Make sure you have the required packages installed. You will need the djangorestframework and drf-yasg packages. You can install them using pip:
pip install djangorestframework drf-yasg
  1. Configure Django settings: Open your Django project's settings file (settings.py) and add the following configurations:
INSTALLED_APPS = [
    ...
    'rest_framework',
    'drf_yasg',
    ...
]

REST_FRAMEWORK = {
    'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema',
}
  1. Generate the OpenAPI schema: In your Django project's root directory, run the following command:
python manage.py generateschema --format openapi --out schema.yml

This command will generate an OpenAPI schema file named schema.yml in the root directory of your project.

  1. View the generated schema: You can now open the schema.yml file to view the generated OpenAPI schema. This file contains the API endpoints, their request and response schemas, and other relevant information.

Note: The --format option specifies the format of the schema to be generated, and the --out option specifies the output file path.

That's it! You have successfully generated an OpenAPI schema for your Django project using the command line.