django-planet

Blog: Adam Johnson

web https://adamj.eu/
Author Adam Johnson

Django: find ghost tables without associated models

Nov. 21, 2024 » Adam Johnson » [Archived Version]

Heavy refactoring of models can leave a Django project with “ghost tables”, which were created for a model that was removed without any trace in the migration history. Thankfully, by using some Django internals, you can find such tables. Use the database introspection methods table_names() to list all tables and …

Read More

Django-related Deals for Black Friday 2024

Nov. 18, 2024 » Adam Johnson » [Archived Version]

Here are some Django-related deals for this year’s Black Friday (29th November) and Cyber Monday (1st December), including my own. I’ll keep this post up to date with any new deals I learn about. If you are also a creator, email me with details of your offer and …

Read More

Boost Your Django DX updated again

Nov. 18, 2024 » Adam Johnson » [Archived Version]

I have just released the second update to Boost Your Django DX, my book of developer experience (DX) recommendations for Django projects. This update contains a new chapter, changes some recommended tools, and upgrades to Python 3.13 and Django 5.1. Overall, the book is 45 pages longer, now …

Read More

Django: Introducing Djade, a template formatter

Sept. 25, 2024 » Adam Johnson » [Archived Version]

Happy DjangoCon US 2024 to you. Whilst I am not there, I have adopted the spirit of the season and got to work hacking together a new tool. Djade is a formatter for Django templates. It applies rules based on the template style guide in Django’s contribution documentation, plus …

Read More

Django: speed up tests slightly by disabling update_last_login

Sept. 17, 2024 » Adam Johnson » [Archived Version]

Django’s test client provides two methods to log in a user: login() and force_login(). The latter one is faster because it bypasses the authentication backend, including password hashing, and just sets a session to have the user logged in. Typically, you’d want to use it in setUp() like …

Read More

Django: hoist repeated decorator definitions

Sept. 7, 2024 » Adam Johnson » [Archived Version]

Django provides us with a rich set of view decorators. In this post, we’ll look at a technique for hoisting repeated use of these decorators to reduce repetition. Repeated @cache_control calls Here are two public views with the same @cache_control decorator: from django.views.decorators.cache import cache_control @cache_control …

Read More

Django: a pattern for settings-configured API clients

Sept. 4, 2024 » Adam Johnson » [Archived Version]

Here’s an example of a common pattern in Django projects: from acme.api import APIClient from django.conf import settings acme_client = APIClient(api_key=settings.ACME_API_KEY) def order_anvil() -> None: acme_client.anvils.order(...) An API client is instantiated as a module-level variable based on some settings. This approach has some drawbacks …

Read More

Django: build a Microsoft Teams bot

Sept. 3, 2024 » Adam Johnson » [Archived Version]

Recently, I built a Microsoft Teams bot for a client, inside their Django project. It wasn’t fun or easy, but the experience did increase my resiliency as a developer. I also went into this forewarned by my wife, a product manager also known as “the integration queen”, who has …

Read More

Django: avoid “useless use of .all()”

Aug. 30, 2024 » Adam Johnson » [Archived Version]

Here’s a little ORM pet peeve of mine that may deepen your understanding of how QuerySets work. Take this code: Digger.objects.all().filter(height_cm__gt=200) The .all() is unnecessary. It’s equivalent to write: Digger.objects.filter(height_cm__gt=200) Why? The manager, Digger.objects, already refers to …

Read More

Django: rotate your secret key, fast or slow

Aug. 30, 2024 » Adam Johnson » [Archived Version]

Django’s SECRET_KEY setting is used for cryptographic signing in various places, such as for session storage and password reset tokens. This makes keeping it secure a high priority since an attacker with the key could forge things like password reset tokens. If you have leaked your secret key, you …

Read More