Django requires every change to model fields and meta classes to be reflected in database migrations. This applies even to things that don’t typically affect the database, such as Field.choices. When iterating on code, it’s easy to make a model change and forget to update the migrations accordingly. If you don’t have any protection, you might even deploy code that crashes due to out-of-date migrations!
Django and other frameworks often allow you to configure classes, functions, or modules to import using strings. To do the same in your own code, use pkgutil.resolve_name() from the standard library (added in Python 3.9):
Sometimes, you want to test a function that uses an inner import and mock that imported object. For example, say you wanted to mock dig() in this example:
Copy-paste-tweaking library code feels like a dirty but inevitable programming practice. Often driven by deadlines or other constraints, it seems all projects end up with something copy-pasted in and tweaked for one specific use case.
Many terminals and text editors support what I’ll call “line number paths” of the form <filename>:<lineno>. Opening that path, whether by clicking or passing to a CLI, opens the given file at that line.
Django’s release notes are extensive and describe nearly all changes. Still, when upgrading between Django versions, you may encounter behaviour changes that are hard to relate to any particular release note.
I have just released an update to my book Boost Your Git DX, six months after its initial release. This update adds some extra content and has a bunch of edits based on reader feedback. The PDF is now ten pages longer, for a total of 363.
Django’s system check framework provides fantastic protection for configuration mishaps. It’s like a targeted linter that runs when you start Django commands. It takes advantage of runtime setup to inspect the true state rather than infer it from the source.
GeoDjango requires the GDAL and GEOS spatial libraries. On macOS, you can use Homebrew to install these, but they won’t be picked up by default since they live in a non-default library directory, /opt/homebrew/lib. Django will fail to start with an exception:
The clipboard is an easy way to transfer data into the command line from other tools. Use your system’s “paste from clipboard” command to pipe data into other commands for processing.
Mastodon is a Twitter-like social network with a solid Django community presence. It’s a fantastic online arena for connecting with others, discovering news, discussing issues, and sharing FOMO-inducing conference photos.
git bisect efficiently searches for a commit responsible for changing a given behaviour. git log lets you see when a given file or line changed, but that’s often insufficient when the cause of some change is unclear. In such cases, git bisect shines, as it lets you check your running system.
pre-commit’s “fail” virtual language fails all files that it matches. Combine it with pre-commit’s file name and type matching to block unwanted files in your repository.
Contrary to common belief, Git doesn’t store diffs. It actually stores snapshots of whole files, heavily compressed to reduce redundancy. Then when displaying a diff is required, git diff generates it on the fly.
I just released a major update to my 2021 book Boost Your Django DX. This update contains new content, a bunch of edits, and uses the latest versions of tools, including Python 3.12 and Django 5.0.
PostgreSQL’s powerful full text search feature supports several query syntaxes. Of these, a website search feature should typically pick the websearch syntax. websearch copies some features from popular search engines, as covered below, offering familiar short syntax to users. It is also forgiving and will never raise a syntax error for user input, whilst other syntaxes can.
My two “Boost Your DX” books are available in a bundle deal, saving $10 compared to buying them separately. Great for improving your Django and Git skills at the same time.
Global Privacy Control (GPC) is a specification for web browsers to signal website operators not to share or sell the user’s data. This signal is intended to exercise legal data privacy rights such as those provided by the California Consumer Privacy Act (CCPA) or the EU’s General Data Protection Regulation (GDPR). While GPC is a proposal, support has been implemented in Firefox and several other privacy-focused browsers and extensions.
A fairly common situation in a Django project is where you need to store and serve arbitrary HTML fragments. These often come from forms with rich text editors (using HTML’s contenteditable).