200 Posts Tagged ‘django’ (Page 3)

(All tags.)


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:

Read more...

Django: Fix version 5.0’s URLField.assume_scheme warnings

Since 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.

Read more...

Django-related Deals for Black Friday and Cyber Monday 2023

Here are some Django-related deals for this year’s Black Friday (24th Nov) and Cyber Monday (27th Nov), including my own.

Read more...

Django: Stop a backgrounded runserver with lsof

If 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.

Read more...

Django: Maybe disable PostgreSQL’s JIT to speed up many-joined queries

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.

Read more...

Boost Your DX bundle deal

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.

Read more...

My fourth appearance on Django Chat

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:

Read more...

Django: Move a template tag library into builtins

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.

Read more...

Django: The perils of string_if_invalid in templates

Django’s template engine has a string_if_invalid option that replaces missing variable lookups with a string of your choice:

Read more...

Django: Clean up unused code with Vulture

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.

Read more...

Django: Flush out test flakiness by randomly ordering QuerySets

Sometimes 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.

Read more...

Django: A version of json_script for pre-serialized JSON strings

Django’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.

Read more...

Django: A security improvement coming to format_html()

Can you spot the problem with this Django snippet?

Read more...

Django: Avoid database queries in template context processors

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:

Read more...

Django: Parametrized tests for all model admin classes

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.

Read more...

Django: How to profile and improve startup time

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.

Read more...

How to migrate from Django’s PostgreSQL CI Fields to use a case-insensitive collation

If you upgrade to Django 4.2, you may see system check warnings like:

Read more...

django-upgrade release with Django 4.2 fixers

I just released django-upgrade 1.13.0. The headline features are some new fixers targeting Django 4.2, which is currently in alpha.

Read more...

Django settings patterns to avoid

Here are some potential mistakes made with Django settings that you can avoid.

Read more...

Django-related Deals for Black Friday and Cyber Monday 2022

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.

Read more...

Python: unittest’s new context methods from Python 3.11 (with backports)

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.

Read more...

django-upgrade Mega Release 1.11.0

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.

Read more...

Migrate PostgreSQL IDs from serial to identity after upgrading to Django 4.1

The Django 4.1 release notes feature this short, innocent-looking note:

Read more...

How to implement a “dry run mode” for data imports in Django

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.

Read more...

How to Safely Pass Data to JavaScript in a Django Template

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!

Read more...