I recently read this interesting Reliable Django Signals article which left me inspired to have a play with building something similar. I wanted build a simple event-driven architecture where: Publishers can publish an event with a given payload One or many subscribers can subscribe to an event Events are asynchronous (as opposed to the synchronous observer pattern ) I took the opportunity to try…
I recently read How to use UUIDv7 in Python, Django and PostgreSQL from Paolo Melchiorre . I particularly liked the section demonstrating how to extend the model with a generated column that stores the timestamp extracted from the UUID. from django.db import models class UUIDv7 (models . Func): function = 'uuidv7' output_field = models . UUIDField() class UUIDExtractTimestamp (models . Func):…
When writing tests I use the example.com domain for any URLs, email addresses, and similar. The reason: if my test unintentionally made a real request, or sent a real email, then it would resolve to a special-use domain. The Internet Assigned Numbers Authority (IANA) reserves the example.com domain for documentation purposes. Better that the request resolves there than to one that’s privately…
I recently came across Capo , a tool that helps identify HTML head elements that are out of order. Using the Chrome extension, it offered a few suggestions for my project. Move the es-module-shims asynchronous script and importmap synchronous script to be a lot higher All the synchronous styles <link rel=stylesheet> to come before deferred scripts <script defer src> Remove some preload elements…
Recently shared at work was this TIL from Samuel, Custom alias for pretty printing in Python debugger with .pdbrc (including Django models!) . Here’s my ever so slightly adapted version which also includes printing a reminder to me that these exist. alias rp import rich; rich.print(%*) alias rpo import rich; from django import forms; rich.print(forms.model_to_dict(%*)) print('Aliases: Use `rp obj`…
I recently had a deployment fail with the following WhiteNoise error. -----> $ python src/manage.py collectstatic --noinput Post-processing 'polls/style.css' failed! whitenoise.storage.MissingFileError: The file 'polls/style.css.map' could not be found with <whitenoise.storage.CompressedManifestStaticFilesStorage object at 0x7f5c4b1970e0>. The CSS file 'polls/style.css' references a file which…
Django has a many built-in database functions and a documented Func API for writing your own. Whilst writing a custom Func subclass may sometimes be necessary, I learnt that there’s many cases when you can instantiate Func with the necessary arguments to get what you need. For example, take the following model. from django.contrib.postgres.fields import ArrayField from django.db import models…
I have some slow running, low priority Celery tasks that I don’t want holding up more important tasks. I learnt how to configure Celery tasks, and queues, to support message priorities. Note, the project in question uses Django, Celery, and RabbitMQ. Firstly, I needed to decide on my priority values . Whilst RabbitMQ supports priorities between 1 and 255, their docs highly recommend using values…
I recently ran into this error uploading images to AWS S3 using the boto3 package. Parameter validation failed: Non ascii characters found in S3 metadata for key “filename”, value: “ACME™ Anvil.jpg”. S3 metadata can only contain ASCII characters. The character in question was ™. Here’s a simplified example of the code. filename = 'ACME™ Anvil.jpg' metadata: dict[str, str] =…
Let’s say I have the following Django model. from django.db import models class User (models . Model): name = models . CharField(blank = True ) And I want to filter those like so. users = User . objects . filter(name__istartswith = 'John' ) That would perform the following query. SELECT 'data_user' . 'id' , 'data_user' . 'name' FROM 'data_user' WHERE UPPER( 'data_user' . 'name' :: text) LIKE…
Here are some notes on how to structure a Django project. It breaks away from structuring a project around Django “apps” and instead uses a clear separation between three core layers (data, domain, and interfaces). Let’s use the following example, an e-commerce site called “Crema” where people can purchase coffee goods. Below is a layout of the fundamental directories. src/ crema/ data/…