fastapi-django
FastAPI with Django ORM
More details are here.
Directory hierarchy
$ tree -L 3 -I '__pycache__|.venv|staticfiles' -P '*.py'
.
βββ manage.py
βββ mysite
βΒ Β βββ __init__.py
βΒ Β βββ asgi.py
βΒ Β βββ settings.py
βΒ Β βββ urls.py
βΒ Β βββ wsgi.py
βββ polls
βββ __init__.py
βββ adapters
βΒ Β βββ __init__.py
βββ admin.py
βββ apps.py
βββ migrations
βΒ Β βββ 0001_initial.py
βΒ Β βββ __init__.py
βββ models
βΒ Β βββ __init__.py
βββ routers
βΒ Β βββ __init__.py
βΒ Β βββ choices.py
βΒ Β βββ questions.py
βββ schemas
βΒ Β βββ __init__.py
βββ tests.py
7 directories, 18 files
models
: Django ORMsrouters
: FastAPI routersschemas
: Pydantic modelsadapters
: Converting Django ORMs to Pydantic models
Installation
poetry install
Before the first run
Migration
./manage.py migrate
Data insertion
./manage.py shell
>>> from polls.models import Choice, Question
>>> from django.utils import timezone
>>> q = Question(question_text="What's new?", pub_date=timezone.now())
>>> q.save()
Run
$ uvicorn mysite.asgi:fastapp --reload
...
$ ./manage.py collectstatic --noinput # generate static files for django admin
...
$ uvicorn mysite.asgi:application --port 8001 --reload # Django
...
$ curl http://localhost:8000/question/
{"items":[{"question_text":"What's new?","pub_date":"2021-03-29T04:18:54.724432+00:00"}]}%
Mount Django asgi application
# in mysite/settings.py
MOUNT_DJANGO_APP = True
Then http://localhost:8000/django/admin
Tools
FastAPI doc
http://localhost:8000/docs
Django admin
http://localhost:8001/admin
Pre-commit hook
pre-commit install
pre-commit run --all-files