52π
Short Answer
Yes itβs possible with WSGIMiddleware.
For example, you can use all Django features (yes admin too) with mounting, with this example code.
import os
from importlib.util import find_spec
from configurations.wsgi import get_wsgi_application
from fastapi import FastAPI
from fastapi.middleware.wsgi import WSGIMiddleware
from fastapi.staticfiles import StaticFiles
from api import router
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
os.environ.setdefault("DJANGO_CONFIGURATIN", "Localdev")
application = get_wsgi_application()
app = FastAPI()
app.mount("/admin", WSGIMiddleware(application))
app.mount("/static",
StaticFiles(
directory=os.path.normpath(
os.path.join(find_spec("django.contrib.admin").origin, "..", "static")
)
),
name="static",
)
Also this one is from WSGIMiddleware documentation, itβs a more straight-forward example (This one is for Flask but it demonstrates the same idea.).
from fastapi import FastAPI
from fastapi.middleware.wsgi import WSGIMiddleware
from flask import Flask, escape, request
flask_app = Flask(__name__)
@flask_app.route("/")
def flask_main():
name = request.args.get("name", "World")
return f"Hello, {escape(name)} from Flask!"
app = FastAPI()
@app.get("/v2")
def read_main():
return {"message": "Hello World"}
app.mount("/v1", WSGIMiddleware(flask_app))
12π
Latest Update
While it is possible in the approach listed below, I genuinely think that we should avoid coupling different frameworks in such a monolith. Doing so could lead to unexpected bugs and make it harder to scale.
Instead, we could build 1 backend service in FastAPI, 1 Django Admin service for example, and then use NGINX to route traffic to these backend services. Using NGINX as a reverse proxy to route traffic to different backend services in production is common anyway.
Integration Of FastAPI With Django (WSGI)
https://github.com/jordaneremieff/django-fastapi-example.git
After hours of searching finally I found a great implementation in the link above. It worked seamlessly for me!
Testing
In order to make this simple to test, below are some few adjustments I made to the above reference:
api/models.py
class Item(models.Model):
title = models.CharField(max_length=50)
description = models.TextField()
# owner = models.ForeignKey(
# settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="items"
# )
api/schemas.py
class Item(ItemBase):
# id: int
# owner_id: int
class Config:
orm_mode = True
POST
curl -d "{\"title\":\"le titre\", \"description\":\"la description\"}" -H "Content-Type: application/json" -X POST http://127.0.0.1:8000/api/items
GET
curl http://127.0.0.1:8000/api/items
- [Django]-Django contrib admin default admin and password
- [Django]-How can I use the variables from "views.py" in JavasScript, "<script></script>" in a Django template?
- [Django]-What is the purpose of adding to INSTALLED_APPS in Django?
6π
Thank you for the awesome answers. Here is a little tweaked answer where I have fixed some imports as well as I have used a model from a Django app.
from fastapi import FastAPI
from fastapi.middleware.wsgi import WSGIMiddleware
from django.core.wsgi import get_wsgi_application
import os
from importlib.util import find_spec
from fastapi.staticfiles import StaticFiles
from django.conf import settings
# Export Django settings env variable
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
# Get Django WSGI app
django_app = get_wsgi_application()
# Import a model
# And always import your models after you export settings
# and you get Django WSGI app
from accounts.models import Account
# Create FasatAPI instance
app = FastAPI()
# Serve Django static files
app.mount('/static',
StaticFiles(
directory=os.path.normpath(
os.path.join(find_spec('django.contrib.admin').origin, '..', 'static')
)
),
name='static',
)
# Define a FastAPI route
@app.get('/fastapi-test')
def read_main():
return {
'total_accounts': Account.objects.count(),
'is_debug': settings.DEBUG
}
# Mount Django app
app.mount('/django-test', WSGIMiddleware(django_app))
Hint: I created a file named app.py
in my Django projectβs root directory and it worked. Here is my directory structure:
.
βββ accounts
βΒ Β βββ __init__.py
βΒ Β βββ admin.py
βΒ Β βββ apps.py
βΒ Β βββ migrations
βΒ Β βΒ Β βββ 0001_initial.py
βΒ Β βΒ Β βββ __init__.py
βΒ Β βββ models.py
βΒ Β βββ tests.py
βΒ Β βββ views.py
βββ app.py
βββ db.sqlite3
βββ project
βΒ Β βββ __init__.py
βΒ Β βββ asgi.py
βΒ Β βββ settings.py
βΒ Β βββ urls.py
βΒ Β βββ wsgi.py
βββ manage.py
And run your FastAPI app:
(myvenv) β project uvicorn --host 0.0.0.0 --port 8000 app:app --reload
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
INFO: Started reloader process [48366] using statreload
INFO: Started server process [48368]
INFO: Waiting for application startup.
INFO: Application startup complete.
Hopefully, this will work for you. Now accessing /django-test
will serve your Django project and /fastapi-test
will serve the FastAPI part.
This configuration serves Django static files too and we can use our Django models in our FastAPI code as well. Iβll test it further and Iβll update this answer if I find any possibilities for improvement.
- [Django]-In django do models have a default timestamp field?
- [Django]-How to submit form without refreshing page using Django, Ajax, jQuery?
- [Django]-How to query as GROUP BY in Django?
6π
As Sumitrhan pointed out in a comment: There is also Django Ninja a Project that uses very similar concepts like Fast API (Routes, Pydantic Model Validation) but simply is a Django App.
Given that the current version of Django supports async views, I see no point in mixing django and FastApi: Only to get the same feature set much more complicated and not very well integrated.
- [Django]-Get last record in a queryset
- [Django]-Can't compare naive and aware datetime.now() <= challenge.datetime_end
- [Django]-'RelatedManager' object is not iterable Django
2π
there is good resource as follows:
- [Django]-Django: why i can't get the tracebacks (in case of error) when i run LiveServerTestCase tests?
- [Django]-Django syncdb and an updated model
- [Django]-New url format in Django 1.9
-1π
There is also Django-ninja. Django-ninja ispired by FASTAPI.
Django-ninja is a web framework for building APIs with Django and Python 3.6+ type hints. I support Django-ninja if you want to use FASTAPI with Django.
Installation:
pip install django-ninja
Usage
In your django project next to urls.py create new api.py file:
from ninja import NinjaAPI
api = NinjaAPI()
@api.get("/add")
def add(request, a: int, b: int):
return {"result": a + b}
Now go to urls.py and add the following:
from .api import api
urlpatterns = [
path("admin/", admin.site.urls),
path("api/", api.urls), # <---------- !]
- [Django]-Django url tag multiple parameters
- [Django]-Warning: Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'
- [Django]-Django Admin: OneToOne Relation as an Inline?