Publish your Django API docs with Scalar
Project description
Django Scalar
Django-Scalar provides an easy way to serve your API docs with Scalar.
Note: This project is still in its early stages. Pull requests are welcome.
Installation
The python package to install is django-scalar. You can install it with pip install django-scalar
or uv add django-scalar based on your preferred python package manager.
Integrate in your Django app
At this stage, it is required that you have your OpenAPI schema published
with DRF-Spectacular under the api/schema/ endpoint.
To integrate django scalar include django-scalar.views.scalar_viewer in your urls.py.
from django.urls import path
from django_scalar.views import scalar_viewer
from drf_spectacular.views import SpectacularAPIView
urlpatterns = [
# ...existing URL patterns...
path("api/schema/", SpectacularAPIView.as_view(), name="schema"),
# Add this:
path("api-docs/", scalar_viewer, name="scalar-viewer"),
]
Feel free to change api-docs/ to your liking. We've seen api/docs etc. in the wild.
Requirements:
There are two main requirements for this project:
- drf-spectacular
- django-filters (optional)
This project implicitly depends on Django and Django-Rest-Framework.
Documentation example:
#views.py
@extend_schema(
responses={200: UserSerializer},
tags=["User"],
description="Retrieve a list of all users",
methods=["GET"],
parameters=get_filter_parameters(UserFilter), ## this get_filter_parameters is using the django-filters base to create the necessary parameters automatically.
)
def list(self, request: Request, *args: Any, **kwargs: Any) -> Response:
return super().list(request, *args, **kwargs)
#filters/user.py
usage of get_filter_parameters
- you need to create a basic or complex django-filter class and then use the script from get_filter_parameters the auto-parse the class to a valid OpenApiParameter
#(app)/filters/users.py
import django_filters
from core.models.user import User
class UserFilter(django_filters.FilterSet):
# Filter by username (case-insensitive exact match)
username = django_filters.CharFilter(lookup_expr="iexact")
# Filter by email (case-insensitive exact match)
email = django_filters.CharFilter(lookup_expr="iexact")
# Filter by full name (case-insensitive partial match)
full_name = django_filters.CharFilter(
field_name="first_name", lookup_expr="icontains", label="Full Name"
)
# Filter by is_active (boolean filter)
is_active = django_filters.BooleanFilter()
# Filter by date joined (range filter for datetime)
date_joined_start = django_filters.DateTimeFilter(
field_name="date_joined", lookup_expr="gte", label="Joined After"
)
date_joined_end = django_filters.DateTimeFilter(
field_name="date_joined", lookup_expr="lte", label="Joined Before"
)
# Filter by genres (many-to-many relationship)
genres = django_filters.ModelMultipleChoiceFilter(
queryset=User.objects.all(), field_name="genres__name", label="Genres"
)
# Filter by trusty (boolean filter)
is_trusty = django_filters.BooleanFilter()
# Filter by fee range (decimal filter)
min_fee = django_filters.NumberFilter(
field_name="fee", lookup_expr="gte", label="Min Fee"
)
max_fee = django_filters.NumberFilter(
field_name="fee", lookup_expr="lte", label="Max Fee"
)
type = django_filters.ChoiceFilter(
choices=[
("music", "Musico"),
("establishment", "Estabelecimento"),
],
)
class Meta:
model = User
fields = [
"username",
"email",
"is_active",
"is_trusty",
"genres",
"date_joined_start",
"date_joined_end",
"min_fee",
"max_fee",
"type",
]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_scalar-0.1.7.tar.gz.
File metadata
- Download URL: django_scalar-0.1.7.tar.gz
- Upload date:
- Size: 22.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1ff691c6f1540067cff9abcdfe218fa7d42246248b0a2f9c1c594f15f9ae7366
|
|
| MD5 |
27e23695f433e02ee820c7a003ee6629
|
|
| BLAKE2b-256 |
166d52ae4866e9989593f5dcd7f66f939eefab68b448b4fa20736522ff3e9d40
|
File details
Details for the file django_scalar-0.1.7-py3-none-any.whl.
File metadata
- Download URL: django_scalar-0.1.7-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d75140b437e4186fd75ce898a4948348442b15e5ca1a14c0e603489d04042a8e
|
|
| MD5 |
c120f8f82bcbb0cfc780c70990555891
|
|
| BLAKE2b-256 |
64979d2c82e2b70cfe10318eed6cfacee08f021e03211889a720aaa534178599
|