3 tricks to optimize the Django ORM performance

Indexes

Django ORM creates indices for primary and foreign keys out of the box. However, if you perform a lot of queries on the rest of the columns (the columns that are used in a filter() method) it makes sense to add indexes to them. This can be accomplished by adding db_index=True or unique=True argument to a model field.

N+1 problem

If you do a lot of cross-table queries (loading data from multiple tables) you might end up with N+1 problem. N+1 problem is when ORM produces an additional query for each associated row…

Should I be familiar with databases when working with Django?

One can definitely build apps with Django without any particular database knowledge. However, some of the problems can be solved more easily when you know how to work with a database. Here are a few example of things to know in order to work with a database effectively:

  • how to perform queries with SQL language
  • how to install, configure and backup the database software
  • how to optimize and tune the database performance

So which database should you learn? If you’re just starting out it doesn’t really matter which database to pick, at this stage you only need to…

Why should I use Django ORM when I know SQL?

The main benefits of using Django ORM instead of SQL is a huge improvement in development speed, code maintenance, security and ease of development in general. Let’s talk about these in detail.

Development speed

Django ORM comes with a lot of built-in tools that speed up the development process dramatically. It supports: seamless schema generation and migration, common data relationships (many-to-many, many-to-one, etc) and queries with them, data validation and integrity, switching between various databases without rewriting code, etc. By using Django ORM you get the benefit of the whole Django infrastructure that is built on top of ORM, like…

How to Create File Download Links in Django

In order to create a download link, we need to create a Django view that would serve the files:

  # views.py import mimetypes ... def download_file(request): # fill these variables with real values fl_path = ‘/file/path' filename = ‘downloaded_file_name.extension’ fl = open(fl_path, 'r’) mime_type, _ = mimetypes.guess_type(fl_path) response = HttpResponse(fl, content_type=mime_type) response['Content-Disposition'] = "attachment; filename=%s" % filename return response ...  

After that add a line to urlpatterns in urls.py which references the view.

  # urls.py ... path(‘<str:filepath>/‘, views.download_file) ... </str:filepath> 

So, all the magic is done by including a…

JavaScript, Django and PostgreSQL timezone handling

Please have a look at the first part, which covers date, time and time zone basics in detail. This is a post about timezones issue I’ve been working on at DjaoDjin. Imagine there is a company with multiple teams distributed across the globe. They are all accessing the same DjaoDjin dashboard to see how well the business is doing. The problem lies in reporting the data accurately based on a user location. We could’ve just sticked with UTC, but presenting the data in a local time is a better user experience. So here is the journey…

Working with time in Python

How is time measured?

Time is usually measured by earth rotation or by astronomical observations. There are a lot of standards which specify how this should be done exactly: GMT, UT, TAI, UTC. In this post we will focus on UTC because of its widespread use in computing.

What time is it now?

The answer to this question will likely be different for people who live in different countries. Various geographical areas have their own time zone and their local time is derived from it.…

How to increase the open files limit on Ubuntu

Increasing file limits might be helpful if you’re running a web server which handles a lot of concurrent connections, because every connection opens a socket and socket is a file. In my case, I needed to increase the open file limit from 1024 to 65536 to make sure that server won’t drop connections in the future. At first, I added this line to /etc/security/limits.conf:

  * - nofile 65536  

Where the first column is the user of the process, in our case wildcard * which makes this a default setting for all processes, the second…

What is the point of using NGINX in front of a Django application

Recently, a friend of mine asked me to tell him more about the rationale behind using NGINX with a Django app and how these two work together. It actually makes perfect sense for someone to raise this question: Python has a builtin web server which can render pages, serve static files on its own. So what’s the point of adding one more component to the stack?

PHP Land

When I started to learn programming, my first language was PHP. The only program I needed to run PHP scripts was Apache. My understanding back then was that Apache can serve static…

Configuring a headless Raspberry Pi

Recently, I got my hands on a brand new Raspberry Pi and was asked to make it usable. I didn’t have an HDMI monitor or keyboard, so I decided to configure it as a headless station. I don’t have an Ethernet cable at home but I have a Wi-Fi router. So, the task was to prepare the Raspberry Pi with MacBook Pro (with card reader), Micro USB cable and 4GB SD card. I picked Raspbian as an OS. There are images available for download on the official site. I used Raspbian Jessie Lite, because the full version wouldn’t…

Creating a test environment for Ansible

I’ve been working with Ansible for the last several days. I’ve been testing my Ansible scripts on DigitalOcean. First, I’d create a fresh $5 droplet, run my script a couple of time against this server and soon after the tests were finished, I’d destroy the droplet. But it wouldn’t end with that, I’d often want to make a quick change in the script and test this change, but the environment is already different and I can’t be sure that my script will work the same on the fresh environment, so I would go and create another droplet and repeat the…

Crawling pages with Scrapy

Scrapy is a powerful library used to build crawlers, scrapers and parsers. It is built on top of Twisted which means that under the hood the networking operations are asynchronous which improves the crawling performance significantly. I’m going to describe…

Securing Ubuntu Instance

So, you’ve just launched your brand-new instance and thinking about what to do next. Cool. So, as you probably heard, it is not a good idea to do your tasks using root user account, that’s why we will create a new one. We want this user to have root privileges, but only occasionally, that’s why we will add our new user to sudo group. sudo is a program which allows users to run commands as if they were superusers, asking for a password every time (every 15 minutes) and logging the commands you’ve entered (usually in /var/log/auth.log). In order…