Blog


Django celery + rabbitmq + redis: Use rabbitmq as broker and redis as results backend

Celery is a task queue with focus on real-time processing, while also supporting task scheduling. RabbitMQ is a message broker. This means it handles the queue of “messages” between Django and Celery. Redis is a key-value based storage (REmote DIstributed Storage). Very fast. Used for results backend. Code for this tutorial is here: https://github.com/tek-shinobi/celeryDj Install […]




Django TDD setup

Unit Tests Setup 1. Create virtual env. pipenv shell 2. Install django pipenv install django 3. Create django project. django-admin startproject tested 4. Create .gitignore file touch .gitignore settings setup a settings file for tests. This will use an in-memory sqlite DB (sqlite3). This is good because we don’t want a PG or MySQL db […]




Python Descriptors

Descriptors are often overlooked part of Python. We are all aware of @property. Descriptors are what power it. So lets take a look at them. Why Descriptors Look at this code:

In this toy example, the getter always capitalizes the name before returning. Now if we wanted to implement exactly same functionality in another […]




Mixins in Django and Django Rest Framework

This is a general post about what mixins are. Mixins are a design pattern found in many languages. In Python, though the language does not support mixins natively,they are implemented using Python’s multiple inheritence model. The mixins are different from inheritence. Inheritence is useful when you want to narrow a scope in the inherited class. […]




Using Pytest with Django and Django Rest Framework

This article is inspired by article here. What is Pytest Pytest is testing framework for Python. Very popular with Django. Killer feature : Fixtures Fixtures are the killer feature of Pytest. Fixtures are functions that run before and after each test, like setUp and tearDown in unitest and labelled pytest killer feature. Fixtures are used […]




Python: Bare asterisk in function arguments?




Adding Custom User model in Django – part 2 (using fixtures)

See part 1 here. In this part we are discussing what to do when we add custom user model later in the project. Note that in this scenario, we are in a very non-ideal situation. To keep unknown surprises at the minimum, we will still delete old database but we will use the concept of […]




Adding AJAX to Python Django projects

A very nice explanation on SO regarding what and why about AJAX from Django perspective: https://stackoverflow.com/questions/20306981/how-do-i-integrate-ajax-with-django-applications So, specifically in Django, when we use AJAX to do a form POST request, we need a CSRF token. This is a security feature. If we are using Django templates, the templates do the job of generating a csrf […]




Steps for new Python library or package

Here are the steps. Create repo on github with license, gitignore and readme stubs. Clone the repo in the work area. Lets say, my new library is called new-library new-library is the repo name. Inside the cloned repo, create virtual env by running: pipenv install –python 3.8 Above step creates a virtualenv using python 3.8 […]




Django sessions for anonymous users

We know that all logged in users are connected to a session. This is something done for you by Django middlewares. Similarly, for anonymous users (not logged in), every time the server receives a request, django creates a session object (meaning, an object with session_key, session_data and expire_data values). But the catch here is that […]