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:

1
2
3
4
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 ...