[Django]-Is it possible to use FastAPI with Django?

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
πŸ‘€William Le

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.

πŸ‘€Rehmat

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.

πŸ‘€Kound

2πŸ‘

there is good resource as follows:

Combine the power of FastAPI and Django

-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),  # <---------- !]
πŸ‘€Myrat_jr

Leave a comment