2General

Want to know more about us? Visit 2general.com »

Splitting up settings in Django

By default all Django settings are in one monolithic settings.py file. A single big file is hard to read and hard to maintain.

Django users have found many ways to split up the settings into multiple files, all of them with their pros and cons. In our latest projects, we have developed yet another way, which uses file inclusion instead of importing Python scripts.

The main features of django-split-settings are:

  1. Settings can be split into files and directories.
  2. Files later in the list can modify configurations, for example add or remove apps from INSTALLED_APPS.
  3. The main settings file lists the files that make up the project’s settings.
  4. Files can be marked optional. Optional files can be used to override settings per instance.
  5. Wildcards can be used in file paths.
  6. Maintains support for Django’s runserver auto-reloading.
Read more...

Changing Django cache backend between test cases

It’s a good practice to run tests for a Django project with a dummy cache backend. This eliminates side effects of one test from affecting the results of other tests.

Here’s how to activate the dummy backend in a Django settings file:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.dummy.DummyCache'
    }
}

However, sometimes it’s also necessary to test how an application uses the cache. In this article, we’ll show how to replace the dummy cache with a real cache backend separately for individual test cases.

Read more...

Selective restore from database backups with Django

Scream by eflon, on Flickr

When things go wrong and you lose your database, backups will help save the day. If the whole production database is corrupted or lost, it’s simple to throw it away and restore it in its entirety from the latest backup.

If data loss caused by a user error has remained unnoticed for some time, valuable data may since have been stored, and restoring a complete backup is not an option. In such cases it’s useful to be able to do a partial restore of one or more tables while keeping the rest of the database.

For these complex cases, I’m going to describe a technique for restoring a subset of the data in one or more relational database tables using Django.

Read more...

Creating stacked bar charts with Flot

Flot is a powerful JavaScript plotting library for jQuery. It uses <canvas> tag for creating beautiful graphical plots. Out of the box it supports lines, points, filled areas, bars and any combinations of these. With plugins you get pie charts, stacked charts and more.

Today we’ll take a look at Flot’s stacked charts support. The first thing you must notice is that you have to pass the data in the right format. Also, Flot has a bug in creating stacked charts that may require some tinkering with Flot’s code. Luckily there’s a patch available.

Read more...

Compiling Dust.js templates with django-mediagenerator

Here at 2General we’ve used django-mediagenerator as the asset manager in many of our latest projects. It combines the best parts of Django’s own staticfiles implementation and the older django-compressor app.

Dust.js is a fast, asynchronous JavaScript-based templating engine which runs both in browsers and on Node.js. We haven’t used Dust.js extensively yet, but we immediately noticed that if we’re planning to use it, we need to compile Dust.js templates during our normal build process, i.e. be able to add them in our mediagenerator bundles.

Read more...