Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kigawas/fastapi-django
FastAPI with Django ORM
https://github.com/kigawas/fastapi-django
Last synced: 2 days ago
JSON representation
FastAPI with Django ORM
- Host: GitHub
- URL: https://github.com/kigawas/fastapi-django
- Owner: kigawas
- License: mit
- Created: 2020-08-21T07:39:01.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-10-01T14:44:08.000Z (3 months ago)
- Last Synced: 2024-10-18T15:06:59.138Z (2 months ago)
- Language: Python
- Size: 513 KB
- Stars: 249
- Watchers: 8
- Forks: 30
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fastapi-django
FastAPI with Django ORM
More details are [here](https://kigawas.me/posts/integrate-fastapi-and-django-orm/).
## Directory hierarchy
```bash
$ 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.py7 directories, 18 files
```- `models`: Django ORMs
- `routers`: FastAPI routers
- `schemas`: Pydantic models
- `adapters`: Converting Django ORMs to Pydantic models## Installation
```bash
uv sync
```## Before the first run
### Migration
```bash
./manage.py migrate
```### Data insertion
```bash
./manage.py shell
``````python
>>> 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
```bash
$ 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
```python
# in mysite/settings.pyMOUNT_DJANGO_APP = True
```Then
## Tools
### FastAPI doc
```plaintext
http://localhost:8000/docs
```### Django admin
```plaintext
http://localhost:8001/admin
```### Pre-commit hook
```bash
pre-commit install
pre-commit run --all-files
```