Recent Posts (Page 13)

Cyber Monday discount for Speed Up Your Django Tests

Earlier this week I tweeted about my two part offer on my book Speed Up Your Django Tests for this year’s Cyber Monday.

Read more...

A Django REST API in a Single File

I previously covered writing a Django application in a single file, for both synchronous and asynchronous use cases. This post covers the angle of creating a REST API using Django in a single file.

Read more...

How to Mock Environment Variables in pytest

Sometimes tests need to change environment variables. This is fairly straightforward in pytest, thanks to os.environ quacking like a dict, and the mock.patch.dict decorator/context manager.

Read more...

Python: mock environment variables with unittest

Sometimes, tests need to change environment variables. This is straightforward in tests using Python’s unittest, thanks to os.environ quacking like a dict, and the mock.patch.dict decorator/context manager.

Read more...

Django: prevent unintended data modification with django-read-only

Last week, I released a new Django package, django-read-only. It provides a read-only mode for Django’s database layer.

Read more...

Django: unit test a management command

When we write custom management commands, it’s easy to write integration tests for them with call_command(). This allows us to invoke the management command as it runs under manage.py, and retrieve the return code, standard output, and standard error. It’s great, but has some overhead, making our tests slower than necessary. If we have logic separated out of the command’s handle() method, it improves both readability and testability, as we can unit test it separately.

Read more...

Django: what happens when you run manage.py test?

You run your tests with manage.py test. You know what happens inside your tests, since you write them. But how does the test runner work to execute them, and put the dots, Es, and Fs on your screen?

Read more...

Django: Bonus documentation sites

There are a few mini sites out there with “bonus” Django documentation. Here’s a list of the best ones I know of.

Read more...

Django and the N+1 Queries Problem

The N+1 Queries Problem is a perennial database performance issue. It affects many ORMs and custom SQL code, and Django’s ORM is not immune either.

Read more...

A Guide to Python Lambda Functions

In Python, Lambda functions are rare compared to “normal” functions, and occasionally misunderstood or overused.

Read more...

Backporting a Django ORM Feature with Database Instrumentation

Last week I covered Django’s database instrumentation, and making a wrapper that’s always installed. Here’s a different use case that I encountered last year on a project.

Read more...

How to Fix a PytestCollectionWarning about WebTest’s TestApp Class

Here’s a small warning I’ve come across a couple of times, and how to fix it with a niche pytest feature.

Read more...

Django: Modernize index definitions with zero downtime

If you’ve read the Django documentation for Model.Meta.index_together recently, you may have noticed this note:

Read more...

How to Make Always-Installed Django Database Instrumentation

Since version 2.0, Django has provided a hook for installing database instrumentation wrapper functions. These functions are like middleware for database queries, allowing you to inspect and rewrite SQL before it is sent to the database. There are many use cases, for example:

Read more...

How to Use Django’s Parallel Testing on macOS With Python 3.8+

Django’s test command takes the --parallel flag to activate parallel testing. Unfortunately, this has never worked on Windows, and with Python 3.8 it has stopped working on macOS.

Read more...

Fun with GPT-3

Here’s a conversation I had earlier with the late Ernest Hemingway:

Read more...

Disable Instrumentation Packages during Tests

Your Django project may use some packages that add instrumentation, for example:

Read more...

Announcing a Regional Discount for “Speed Up Your Django Tests”

Since I launched my book Speed Up Your Django Tests I’ve had a number of questions about possible discounts. I want to make my book affordable for all and understand that $49 is a high price tag for some. The Django community is global and I want to share knowledge with Djangonauts everywhere.

Read more...

Better Python Decorators with wrapt

A Python decorator wraps a target function with another wrapper function. This wrapper function can add any behavior you might want. For example, it can track execution times, redefine how the wrapped function runs, or modify return values.

Read more...

Why does Python raise ModuleNotFoundError when modifying Django’s INSTALLED_APPS?

Imagine we are installing the third party package django-cors-headers, which I maintain. Step one in its installation process is to install the package, so we run the command:

Read more...

Django: Add a .well-known URL

The /.well-known/ URL path prefix is a reserved name space for serving particular static files used by other systems that might interact with your site. It was established by RFC 5785 and updated in RFC 8615. The IANA maintains a list of possible files in the Well Known URIs registry, but others are in wide use without official registration (yet?).

Read more...

How to Check if Python’s Output Buffering Is Enabled

By default, Python buffers output to standard output (stdout) and standard error (stderr). This means that output from your code might not show up immediately, making debugging harder.

Read more...

Python: fix SyntaxWarning: list indices must be integers or slices

Take this code, which we want to return a list of two breakfast orders:

Read more...

Python: fix SyntaxWarning: assertion is always true

Take this code:

Read more...

Python: fix SyntaxWarning: '<type>' object is not subscriptable

Take this code:

Read more...