Flask vs django | easy expert comparison

Both Flask and Django are:

What is the difference Flask vs Django? TL;DR:

Github stars

Django:

Django github stars

Flask:

Flask github stars

Django Internal ORM

1. Define the model in models.py with e.g.

class Person(models.Model):
    name = models.CharField(max_length=30)
    age = models.IntegerField()

2. Run python manage.py makemigrations

3. Run python manage.py migrate

That is all! It will generate CREATE TABLE SQL statement and execute it under the hood to synchronize your models to the database.

Then just do:

p = Person(name="Alex Kotli", age=28)
p.save()

And Djangos ORM will perform INSERT SQL query under the hood. If you will write:

Person.objects.filter(age__gt=18)

And it will do SELECT SQL which will return an iterable with all persons with age greater than 18.

Flask vs Django ORM

Flask doesn't have ORM.

You can pip install side-vendor ORM called SQLAlchemey, and then connect it by imports or flask plugins. But SQLA itself does not have migrations, so you will then need an Alembic for migrations, and then you will need to configure both (several files but it takes time anyway)

Django admin area

Create a boilerplate app, run python manage.py createsuperuser and then log in to http://127.0.0.1:8080/admin/:

Django admin area look

It already allows you to manage users/roles/permissions and could be customized for any new model! Flask doesn't have it.

Popular companies who use it

Django is used by the next big players:

Companies which use Flask

Companies using Flask:

Companies which use Flask

As you can see both frameworks have great trust in huge companies.

What to choose Flask or Django?

Make your choice by considering one by one all pros and cons.

Django benefits, Flask disadvantages

Flask pros, Django cons

Is there something faster than Flask and Django?

The worst disadvantage of both frameworks that they implement a blocking model, so to serve 4 requests at the same time you have to run the framework in some multi-thread/multi-process runner like gunicorn. If it has 4 active workers and they are all busy handling requests, the 5th request will wait until some worker will get free.

To fix this issue Python core developers in 3.6+ versions introduced so-called asyncio programming model (you can often hear async/await) which has its own HTTP frameworks:

Both have the same simple functionality as a flask (They are microframeworks). Also, this model requires all used IO-related libraries to be compatible with async/await, for example instead of SQLALchemy you need to use Gino ORM. Instead of requests you will need aiohttp client. But you can still use Alembic because migration efficiency is not important at all.

So here we come to another benefit of Flask – if you will want to implement asyncio model, with Flask it will be much easier to adopt your views and APIs.

Comparing to the blocking model, async approach is new, and we believe that it will squeeze out the legacy way in our IO-heavy world due to great efficiency. If you want to understand asyncio model on a simple example, please read the hint Parallel execution of asyncio functions.

#python #django #flask
15
Ivan Borshchov profile picture
Oct 22, 2020
by Ivan Borshchov
Did it help you?
Yes !
No

Best related