70

I'm a Django developer and recently stumbled onto the FastAPI framework.

Then I decided to give it a shot. But usually when you talk about building RESTful APIs with Django you usually use the Django Rest Framework (DRF).

Is anybody aware if it is possible to substitute DRF by FastAPI using Django perks, like its ORM, and still have access to all of FastAPI's async features?

Up until now I only found one article on this. But in the process of integration the author lost most of the features of FastAPI. You can find it here.

In the FastAPI docs, they do mention that it is possible to redirect certain request to a WSGI application here.

Leonardo Guerreiro
  • 901
  • 1
  • 9
  • 18
  • 2
    What feature of FastAPI would you like to have in Django (or in DRF)? – JPG Sep 03 '20 at 15:58
  • 12
    https://django-ninja.rest-framework.com/ is an alternate for DRF, which is built on top of FastAPI. – Sumithran Sep 30 '20 at 14:28
  • 9
    @Sumithran According to the [https://github.com/vitalik/django-ninja/blob/master/pyproject.toml](pyproject.toml) Django ninja does not require FastAPI and also their documentation states `This project was heavily inspired by FastAPI`. So it is a parallel development, not built on top of FastAPI – Kound Jan 27 '21 at 15:24
  • 1
    yes, an example: https://www.stavros.io/posts/fastapi-with-django/ – gregory Feb 01 '21 at 06:54
  • 28
    I don't understand who are the ones deciding to close a question like this. Sometimes the despotic behaviour of the "moderators" on SO is indignant. This is a perfectly valid and useful question. – Guillermo Brachetta Mar 05 '21 at 20:04
  • The only thing preventing me to use FastAPI is that I didn't want to use another ORM and learn again everything. Django ninja is exactly what I needed for simple APIs. Best of both worlds! thanks guys! Hope the projets gains some traction. – David Dahan Jul 14 '21 at 20:44
  • Voted the question to reopen. – Yagiz Degirmenci Jul 23 '21 at 12:00

6 Answers6

51

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))
lcsvcn
  • 1,184
  • 3
  • 13
  • 28
Yagiz Degirmenci
  • 16,595
  • 7
  • 65
  • 85
  • 1
    Appreciate bro! I'll try it out! But it seems neat. So, this line `app.mount("/admin", WSGIMiddleware(application))` would reference to the /admin on the urls.py of my django application? – Leonardo Guerreiro Sep 03 '20 at 16:39
  • 5
    @LeonardoGuerreiro Yup. Also this [repo](https://github.com/jordaneremieff/django-fastapi-example) could be useful for you – Yagiz Degirmenci Sep 03 '20 at 21:41
  • 1
    when I try to acess django's admin page, it is not serving the static files well. Do you know what might be causing that? – Leonardo Guerreiro Sep 17 '20 at 12:56
  • The initial page of the admin is ok, but when I access the inner pages, for example "Users", it is not able to load the static files: `INFO: 127.0.0.1:48012 - "GET /static/admin/img/tooltag-add.svg HTTP/1.1" 404 Not Found INFO: 127.0.0.1:48004 - "GET /static/admin/img/sorting-icons.svg HTTP/1.1" 404 Not Found` – Leonardo Guerreiro Sep 17 '20 at 13:27
  • btw, do you know if there is anything like django's admin page for FastAPI? It's not required that I use the django's ORM and admin page, I just don't wanna do it myself. – Leonardo Guerreiro Sep 17 '20 at 13:29
  • Yup, i really like [Dashboard](https://github.com/encode/dashboard) encode behind this project(Creators of DRF-Starlette-Uvicorn-HTTPx etc) – Yagiz Degirmenci Sep 17 '20 at 14:09
  • But if you are okay for a tortoise, fastapi-admin is a good option tho. – Yagiz Degirmenci Sep 17 '20 at 14:10
  • `STATIC_URL = "/static/" # Media files MEDIA_URL = config("MEDIA_URL", default="/media/") MEDIA_ROOT = os.path.join(BASE_DIR, "media") # Static Files STATIC_ROOT = os.path.join(BASE_DIR, "static")` – Leonardo Guerreiro Sep 17 '20 at 14:55
  • 1
    I managed to fix it. Turns out that I only needed to add `app.mount("/static", StaticFiles(directory="static"), name="static")` to my `asgi.py` file inside the function where I call de FastAPI instance and refer it to my STATIC_ROOT on the settings.py. checkit out [here](https://fastapi.tiangolo.com/tutorial/static-files/) – Leonardo Guerreiro Sep 17 '20 at 17:01
  • what is that ` ".."` thing do in the line `os.path.join(find_spec("django.contrib.admin").origin, "..", "static")`?? – Ali Husham Jun 29 '21 at 09:11
  • Please use google for these such things instead of necrobumping, see: [What is double dot(..) and single dot(.) in Linux?](https://stackoverflow.com/questions/23242004/what-is-double-dot-and-single-dot-in-linux#:~:text=On%20Unix%2Dlike%20operating%20systems,the%20directory%20that%20contains%20it) – Yagiz Degirmenci Jun 29 '21 at 10:18
  • help please I got `ModuleNotFoundError: No module named 'myapp.settings'` with `os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")` when I run `uvicorn myapp.myapp.wsgi:app --port 8001 --host localhost` – Ali Husham Jul 10 '21 at 15:00
  • I don't know why you are necrobumping, the problem you are facing is unrelated to the question and violates SO rules @alial-karaawi. – Yagiz Degirmenci Jul 10 '21 at 15:44
  • @YagizDegirmenci I used the cope of the answer and I copied it and paste it and modified it to suits my project then I got the issue so it is reallited – Ali Husham Jul 11 '21 at 13:03
  • i have concerns about connections created by django orm because normally django manage the db connection in request-response cycle which is not works well with starlette requests, i think the connections created by fastapi endpoints not gonna be closed as excepted . – Veli Eroglu Oct 25 '21 at 22:51
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
  • 825
  • 1
  • 9
  • 16
7

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
  • 1,835
  • 1
  • 17
  • 30
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/32254130) – Alexander Jul 15 '22 at 11:32
  • 2
    The question asked for "possible to substitute DRF by FastAPI" while **still have access to all of FastAPI's async features**. This is what my answer actually answers. The author wants to use FastAPI features in django. This is what Ninja provides. – Kound Jul 15 '22 at 11:36
  • It's a review suggestion, take it or leave it, it's up to you. – Alexander Jul 15 '22 at 11:40
  • Dude, using FastAPI will bring in one of the greatest joys to your life as you will avoid using the nastiness of DRF, if you can even call that that. – juan Isaza Oct 25 '22 at 19:58
  • I wouldnt call DRF nasty. FastAPI is a delight, but DRF is an absolute powerhouse of a framework. It just takes a bit of reading and a bit of uncomfortable boilerplate to get working. That said, although its a little bit undercooked, it really needs another good year of dev to be what I'd consider stable, Ninja does bring a tonne of ergonomics (and OpenAPI stuff) to the table. – Shayne Feb 09 '23 at 04:14
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
  • 4,681
  • 3
  • 22
  • 38
2

there is good resource as follows:

Combine the power of FastAPI and Django

0

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
  • 1
  • 3
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34476714) – Samuel RIGAUD May 31 '23 at 18:49