Articles Tagged database

Trap in counting related objects in Django

Trap in counting related objects in Django

Task: for every object count number of related objects satisfying some conditions.


class Category(models.Model):
    title = models.CharField(max_length=50)

class Article(models.Model):
    title = models.CharField(max_length=50)
    category = models.ForeignKey(Category)
    approved_at = models.DateTimeField(blank=True, null=True)

Pay attention at field Article.approved_at, it contains article approval time and it can be null.

Create test data:

from django.utils import timezone

c1 = Category.objects.create(title='c1')
c2 = Category.objects ...

Posted on 2017-01-14

Trying JSON in Django and PostgreSQL (and compare with MongoDB)

Trying JSON in Django and PostgreSQL

New JSONField will be added in Django 1.9, it can be used with PostgreSQL >= 9.4. Let's try to work with it and find out, in what situations it can be useful.

Currently django 1.9 alpha is available, final version is scheduled on December 2015. Alpha can be installed with pip:

pip install --pre django

Now imagine that we have an e-commerce site, where we offer products of different types. For example ...

Posted on 2015-09-30

Debug SQL in django test

Debug SQL in django test

In django tests we can measure number of sql queries:

def test_home(self):
    with self.assertNumQueries(1):
        response = self.client.get('/')
    self.assertEqual(response.status_code, 200)

If code in context of assertNumQueries will make other number of DB attempts than expected (here 1), test will throw error. But when test fails it is sometimes hard to understand, what unexpected query was made. To debug such case very useful to log SQL expressions to console. Below ...

Posted on 2015-03-04

Timestamp and ObjectId in mongoDB

Timestamp and ObjectId from mongoDB

Every record in mongoDB has field _id, that must be unique inside collection. By default type of this field is ObjectId, and it is assigned automatically if field is not set.

Lets look at ObjectId more carefully.
It is 12 bytes that includes:

  • a 4-byte value representing the seconds since the Unix epoch,
  • a 3-byte machine identifier,
  • a 2-byte process id, and
  • a 3-byte counter, starting with a random value.

As we see, first 4 ...

Posted on 2015-01-18

Nested SQL queries in Django

Nested SQL queries in Django

Did you know, that Django ORM can do nested SQL queries? Shame on me, but i've found it not so long ago.

So, lets say we have such models of Nursery and Pet:

class Nursery(models.Model):
    title = models.CharField(max_length=50)

class Pet(models.Model):
    name = models.CharField(max_length=50)
    nursery = models.ForeignKey(Nursery, related_name='pets')

We need to get all pets, that are placed in given nurseries. For example, in nurseries with ...

Posted on 2014-09-09

What you should know about mongodb indexes

What it is needed to know about mongodb indexes

Recently i've completed course "M101P: MongoDB for Developers" (periodically repeats, next starts at April). During this course i've found to myself interesting features of mongodb.

1. Index selection.

Suppose we have collection with such document format:

{ "_id" : ..., "a" : 81810, "b" : 97482, "c" : 44288 }
{ "_id" : ..., "a" : 11734, "b" : 27893, "c" : 19485 }
// and so on.

Total 99999 documents. Collection has indexes:{a: 1, b: 1, c: 1}){c: -1 ...

Posted on 2014-03-07

Django queryset.count cache

Django queryset.count cache

Once i mentioned, that my django application makes several similiar queries like 'SELECT COUNT(*) ...'. As it turns out (for me it was surprise), queryset.count() has not obvious cache. But let me start the story from the beginning (and sorry for my english :) ).

As it is known, django's queryset is lazy and have cache.

Lets say we have such model:

class Item(models.Model):
    name = models.CharField(max_length=50)

When some django query is ...

Posted on 2013-07-11

Django admin site optimisation

Django admin site optimisation

It is known, that less amount of database attempts leads to better performance. Usually admin page - part of site with low traffic, but anyway it is good, if no additional database calls happen there. It is more pleasant to use it, because page renders faster and also frees server resources.

In this post i'll try to reduce some database attempts in admin page, when model method __unicode__ contains related object field. I suppose, that ...

Posted on 2013-04-12