Django (18)


Django and Django Rest Framework Permissioning system

What are Permissions In Django Permissions can apply to Individual Users Groups of Users Permissions are all about Access – Who can see what? Control – Who can do what In django docs, Permissions are defined like so: Permissions [are] binary (yes/no) flags designating whether a user may perform a certain task So, the above […]




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

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 running i/o […]




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 […]




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 […]




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 […]




_set meaning in Django ORM relationship

_set is associated with reverse relation on a model. Django allows you to access reverse relations on a model. By default, Django creates a manager (RelatedManager) on your model to handle this, named <model>_set, where <model> is your model name in lowercase. Excellent link on StackOverflow here: https://stackoverflow.com/questions/25386119/whats-the-difference-between-a-onetoone-manytomany-and-a-foreignkey-field-in-d If we have these models:

In […]