Steps for adding ImageField in Django, Django Rest Framework

1. pip install pillow 2. modify the task app model and add ImageField to handle images 3. modify serializer for image and file field 4. create a folder in base project directory to store MEDIA data (i.e.: images, uploaded files) 5. Add MEDIA related settings (MEDIA_ROOT & MEDIA_URL) ins file. 6. Modify in […]

Flask globals – request context – application context – session

I have come to Flask from Django and in this post I am writing down my thoughts as I get more comfortable with Flask globals. Request and Application Context from flask import request, current_app, g request context In that simple line, we have imported three global objects. One is the request object and other two […]

Python Poetry – dependency manager plus packaging tool

I was hearing about this awesome new kid on the block called Poetry. I had taken a look on it in the past (approx two years ago) and it looked not as finished as pipenv, so had stuck with pipenv + But over the weekend, I decided to take another look at it after […]

Secret Key generation for Django and Flask

Both Django and Flask rely on SECRET_KEY to generate things like session IDs, cookies etc. Here is a safe way to generate them. Note that this relies on the secrets module introduced in Python 3.6 and onwards. From the Python docs: The secrets module is used for generating cryptographically strong random numbers suitable for managing […]

Git Stash — way to safely split your commits

  • April 24, 2020
  • Git

A junior dev in my team was regularly committing 15 to 20+ files in one go, with useless messages like “added files”, “changed files” or even better “change 1” etc. Figured out that the reason was, “how to commit until everything is working”. Turns out, he was treating Git as SVN or IBM-clearcase. If you […]

Flask + SQLAlchemy tips

First and foremost: this : This is the old Flask website built in flask. Some patterns are outdated. But still pretty nice to gloss over with a cup of tea and some buiscuits. 🙂 scoped_session(sessionmaker()) or plain sessionmaker() ? Well, the recommended pattern is to use scoped_session as it is considered thread safe. […]

Install psycopg2 on Ubuntu 18.04+

I was trying to install psycopg2 database adapter for PostgreSQL on Ubuntu 18.04. And was always getting an error when doing “pipenv install psycopg2”, complaining about “Python.h” missing. After much head-scratching, I found the issue. Multiple Python distributions confuse psycopg2 installation. Its like this. psycopg2 has two main dependencies libpq-dev python3-dev. On Ubuntu, you install […]

Configure Pycharm Community Edition to run Flask

Setup Environment Step 1: pipenv install –python 3.8 Step 2: pipenv shell Step 3: pipenv install flask flask-sqlalchemy flask-marshmallow marshmallow-sqlalchemy flask-migrate psycopg2 flask-sqlalchemy is the sql-alchemy with flask bindings. flask-marshmallow is like the Serializer in Django Rest Framework, marshmallow-sqlalchemy is common binding between marshmallow and sqlalchemy (for things like ModelSerializer in Django Rest Framework).flask-migrate is […]

SQL Alchemy tutorial

This describes the basic overview of SQL Alchemy. Notice the color theme. It will be related to the next onion-ring overview of SQL Alchemy. SQL Alchemy is divided into Core and ORM. You can use Core and not use ORM. ORM is more geared towards mapping model objects to Python data types. Core on the […]

PostgreSQL Basics

Schema PostgreSQL Schema: Exactly like namespaces in SQL Server (dbo in SQL Server is equivalent to public in PostgreSQL. The default namespace.) Logical container of tables and other database objects You can have multiple schemas in a database Schemas (or namespaces) are useful in production environment in that we can assign users to them and […]