Moving a Django model to another app

Date Modified Tags python / django

Moving a Django model to another app can be tricky. Here is a quick rundown of the most convenient way I found so far. The idea is to simply rename the database tables, and force Django to consider this the new state of your models. Thanks to Marten Kenbeek who ...

more ...

Debugging Django production issues

Date Modified Tags django / python

From time to time, I run into a problem that only occurs in a production-like Django setup (DEBUG off, running under gunicorn, static asset compression in play, and so on).

Debugging such issues on a staging system can be time-consuming. Sometimes, there is no alternative. But often, it's easier ...

more ...

Using a pip cache on GitLab CI

Date Modified Tags gitlab / python

Using a pip cache on GitLab CI can speed up your builds considerably (my build time went down from 8 minutes to 3 minutes). Here is how to do it:

# .gitlab-ci.yml
image: python:2


    - "$CI_PROJECT_DIR/pip-cache"
  key: "$CI_PROJECT_ID"

    # Make sure you ...
more ...

Django static files (and templates) in a nutshell

New to Django and confused about how to handle templates and static files? Don't worry, you are in good company. Many people get confused by this at some point.

Here is a quick example of how to handle your static files and templates in Django 1.8.

1   Settings ...

more ...

Inspecting calls on Python mock objects

Sometimes, you want to assert that an object was called with some arguments, but the exact value for an argument is computed by some external logic that you do not want your test to depend on. Let's say it is generated by a template, and in this particular case ...

more ...

Dynamic forms for Django's admin

A question that pops up every once in a while is how to create Django admin forms dynamically.

This is actually trivial, thanks to the fact that Python has closures. But the fact that it is so simple may not be obvious to everybody, so I am documenting it here ...

more ...

Per-request caching in Django

One of my Django projects contains a function that looks like this:

def get_default_language_code():
    default_lang = Language.objects.filter(default=True).first()
    if default_lang is not None:
        return default_lang.code

This function gets called multiple times, and it will hit the database on each call. This is ...

more ...