django generate openapi schema command line
To generate an OpenAPI schema in Django using the command line, you can follow these steps:
- Install the necessary packages: Make sure you have the required packages installed. You will need the
djangorestframework
anddrf-yasg
packages. You can install them using pip:
pip install djangorestframework drf-yasg
- 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',
}
- 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.
- 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.