A lot of people are using one of Django or Flask, and some are even strongly opiniated about either framework to build a REST API. I moved from Flask to Django a few months ago, and I am never going back. I want to see if I can make other people take a good decision!

The Django ORM knows your database in-and-out

If you played around with Flask, you were probably trying to serve some functionality requiring data, and therefore a database. You probably started with SQLite, because configuring Flask to a database looked a bit confusing or at least time-consuming, because you probably rely on an ORM like SQLAlchemy to manage your database connection, unless you feel like going rogue and using f-strings to write SQL queries (good luck with that!). And because you started with SQLite, you struggled when you needed something more professional like MySQL or PostgreSQL while doing the switch.

With Django, you get your SQL connection for free. Just follow the documentation that tells you which database engine you need, whether you need MySQL, PostgreSQL, MariaDB, Oracle, or even SQLite for toy projects. The engine manages your connection for you.

You want a database table? If you know basic python classes, you have all you need! No need to write queries to create your tables! Specify a class inheriting from django.db.models.Model to define a database table, class attributes to define all possible column types in a very customizable way, allowing you to track with one-liners features such as:

  • Integer fields, character fields, text fields, boolean fields, multiple-choice fields, float fields… and for PostgreSQL, you even get the JSONField.
  • Foreign keys management, so that you know what happens to a relationship when the related model is deleted (no more dead relationships!)
  • Column definition re-usage: just construct fields as functions, and call them to define new model columns!
  • Model inheritance managed in a very powerful way via python class inheritance (there are some pitfalls here, but with a good documentation read, you can avoid all of them).

Can you even imagine solving the problems those features solve by using SQLAlchemy? Personally, I would find that a big waste of time!

#django #django-rest-framework #data-science #python

Why you should choose Django over Flask for your next API project
1.55 GEEK