Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use FastAPI with Django?

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.

like image 852
Leonardo Guerreiro Avatar asked Sep 03 '20 14:09

Leonardo Guerreiro


People also ask

Can FastAPI replace Django?

If you intend to build a complex web portal, a CMS, or any web app with tightly coupling between front and backend, don't even spend time in FastAPI, stick with Django. You can say without hesitation, that FastAPI is a competitor of DRF, Django Rest Framework, instead of the more common known Django project.

Is FastAPI better than Django?

In conclusion, Django is perfect if you want to build robust full-stack web applications because it has several functionalities and works very well in production. On the other hand FastAPI is perfect if you're looking for high performance or scalable applications.

Is FastAPI better than Flask?

When you're building APIs, FastAPI will be a better choice than Flask, especially when microservices are taken into consideration. The only argument for choosing Flask in this case would be if your organization already has a lot of tooling built around that framework.

Is FastAPI worth learning?

FastAPI is a crucial element that brings Starlette, Pydantic, OpenAPI, and JSON Schema together. FastAPI helps in validating the datatype from the developer even in nested JSON requests. It gives the autocomplete feature which helps in producing applications with less amount of effort and also less time in debugging.


2 Answers

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)) 
like image 107
Yagiz Degirmenci Avatar answered Sep 19 '22 19:09

Yagiz Degirmenci


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.

like image 34
Rehmat Avatar answered Sep 19 '22 19:09

Rehmat