• Stars
    star
    247
  • Rank 164,117 (Top 4 %)
  • Language
    Python
  • License
    MIT License
  • Created over 4 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

FastAPI with Django ORM

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 ORMs
  • routers: FastAPI routers
  • schemas: Pydantic models
  • adapters: 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