Django: Defer a model field by default
Some models have one or a few large fields that dominate their per-instance size. For example, take a minimal blog post model:
(All tags.)
Some models have one or a few large fields that dominate their per-instance size. For example, take a minimal blog post model:
URLField.assume_scheme warningsSince Django’s inception, the web has gradually moved from HTTP to HTTPS, a welcome move for security. But the history has meant older parts of Django have had a lingering HTTP bias. Many of these have been migrated to default to HTTPS instead in previous versions. Django 5.0 starts the migration of another, tiny HTTP bias in forms.URLField.
Here are some Django-related deals for this year’s Black Friday (24th Nov) and Cyber Monday (27th Nov), including my own.
runserver with lsofIf you start runserver but your terminal session gets closed, the server can continue running in the background. For example, this can happen when your IDE crashes. This situation is annoying because you can’t stop the backgrounded runserver with Ctrl-C.
Here’s a write-up of an optimization I made in my client Silvr’s project. I ended up disabling a PostgreSQL feature called the JIT (Just-In-Time) compiler which was taking a long time for little benefit.
I released Boost Your Git DX nearly two weeks ago. It’s the second in my “Boost Your DX” series, following last year’s Boost Your Django DX. Both books aim to improve your development experience with their respective tool.
I’ve once more had the pleasure of joining Carlton and Will on the Django Chat podcast, in Episode #146. We spoke on Tuesday, just hours after Django 5.0 alpha 1 was released, on several topics:
Django’s template engine has an underappreciated builtins option that selects libraries to preload in every template. Making a library a builtin avoids the need for an explicit {% load %} tag whenever you use its tags or filters. Putting key libraries in builtins can shorten your templates and make development a little bit faster.
string_if_invalid in templatesDjango’s template engine has a string_if_invalid option that replaces missing variable lookups with a string of your choice:
As projects evolve, old functionality gets removed. Often such deletions are incomplete, leaving in their wake unused functions, classes, and other code objects. Unused code is clutter that brings no joy: it hinders comprehension, taxes codebase-wide quality improvements, and can sometimes lead to errors if later used.
QuerySetsSometimes code depends on the order of a QuerySet whilst not specifying an order. This can lead to random, flaky test failures because databases can return rows in any order when none is specified. The problem is made worse by some databases, notably PostgreSQL, which nearly always return rows in insert order, but occasionally use a different order when a table has had recent deletions.
json_script for pre-serialized JSON stringsDjango’s json_script template filter is a convenient and safe way to pass a larger amount of data to JavaScript. I covered it in my post last year How to Safely Pass Data to JavaScript in a Django Template.
format_html()Can you spot the problem with this Django snippet?
Django’s template engine allows you to augment template contexts with context processors. These are functions that take the current request and return a dictionary to be merged into the context:
Here’s an application of “test smarter, not harder”, as per Luke Plant’s post. I came up with this recently whilst working on my client Silvr’s project, and I’m pretty proud of it. It should apply to any project using Django’s admin.
Your Django project’s startup time impacts how smooth it is to work with. Django has to restart your project every time you run a management command and when runserver reloads. This involves importing all your apps, and thus all the modules that they import.
If you upgrade to Django 4.2, you may see system check warnings like:
I just released django-upgrade 1.13.0. The headline features are some new fixers targeting Django 4.2, which is currently in alpha.
Here are some potential mistakes made with Django settings that you can avoid.
Here are some Django-related deals for this year’s Black Friday (25th Nov) and Cyber Monday (28th Nov), including my own. For more deals on general Python-related products, see Trey Hunner’s post.
Python 3.11 only made one change to unittest, but it’s a good one: context manager methods. These methods can simplify setup and teardown logic in many cases, such as dynamic use of unittest.mock.
I just released version 1.11.0 of django-upgrade, a tool for automatically upgrading your Django project code. This release contains a lot of new features and fixes, thanks to new contributors including those at the Djangocon Europe sprints. Let’s look at the top changes.
The Django 4.1 release notes feature this short, innocent-looking note:
In data import processes it’s often useful to have a “dry run” mode, that runs through the process but doesn’t actually save the data. This can allow you to check for validity and gather statistics, such as how many records already exist in the database. In this post, we’ll look at how to implement a dry run mode in Django by using a database transaction and rolling it back.
You want to pass your data from your Django view to JavaScript, in your template. And, you want to do it securely, with no risk of accidentally allowing malicious code injection. Great, this is the post for you!