Talks - Mia Bajić: Combining Django ORM & FastAPI in a Single App

Learn how to effectively combine Django ORM with FastAPI in a single app, handle async/sync challenges, manage testing complexities, and choose the right framework for your needs.

Key takeaways
  • FastAPI’s main advantages are exceptional performance and async capabilities, while Django excels at providing a complete batteries-included framework with extensive tooling

  • When combining Django ORM with FastAPI, managing sync/async code adds complexity and commonly triggers runtime exceptions that require careful handling

  • Django ORM started adding async features in version 4.2 (April 2023), but full async implementation is still ongoing - making it challenging to use with FastAPI’s async-first approach

  • Common integration challenges include:

    • Tools/packages not working out of the box
    • Limited community resources for troubleshooting
    • Connection management issues between frameworks
    • Need for custom decorators and workarounds
  • Django Ninja offers a potential alternative that bridges FastAPI and Django worlds, providing benefits of both frameworks

  • Testing requires special consideration:

    • Need to handle database fixtures carefully
    • Some testing tools like pytest-django may not work
    • Must manage both sync and async contexts
    • Dependency injection helps with mocking external services
  • Project structure recommendations:

    • Keep database and API layers separate
    • Consider module-based structure for larger projects
    • Use Docker for deployment
    • Follow FastAPI best practices repo guidance
  • Framework choice should depend on requirements:

    • Use Django for CRUD-heavy apps with admin needs
    • Choose FastAPI for performance-critical applications
    • Consider Django Ninja for new projects wanting both
  • No significant dependency conflicts occur between Django and FastAPI as they operate independently despite being in the same application

  • While the integration is possible, conventional setups (Django+DRF or FastAPI+SQLAlchemy) are recommended unless specific requirements justify the added complexity