• Trends
  • Topics
  • Nodes
Search for keywords, #hashtags, $sites, add a dash to exclude, e.g. -$theonion.com

From adamj.eu

Django: a pattern for settings-configured API clients - Adam Johnson

1 1

Here’s an example of a common pattern in Django projects:

#django

on Sep 5

From adamj.eu

Django: build a Microsoft Teams bot - Adam Johnson

1 2

Recently, I built a Microsoft Teams bot for a client, inside their Django project. It wasn’t fun or easy, but the experience did increase my resiliency as a developer. I also went into this forewarned by my wife, a product manager also known as “the integration queen”, who has experienced the...

on Sep 4

From adamj.eu

Django: find ghost tables without associated models - Adam Johnson

0 1

Heavy refactoring of models can leave a Django project with “ghost tables”, which were created for a model that was removed without any trace in the migration history. Thankfully, by using some Django internals, you can find such tables.

on Thu, 9AM

From adamj.eu

Git: count commits with rev-list - Adam Johnson

0 1

git rev-list lists details about commits (also known as “revisions”, hence the name). Its --count option outputs the count of commits in the given range. Pass it @, the short alias for HEAD, to count commits on the current branch:

on Wed, 12PM

From adamj.eu

Django-related Deals for Black Friday 2024 - Adam Johnson

0 0

Here are some Django-related deals for this year’s Black Friday (29th November) and Cyber Monday (1st December), including my own.

on Nov 18

From adamj.eu

Boost Your Django DX updated again - Adam Johnson

0 0

I have just released the second update to Boost Your Django DX, my book of developer experience (DX) recommendations for Django projects. This update contains a new chapter, changes some recommended tools, and upgrades to Python 3.13 and Django 5.1. Overall, the book is 45 pages longer, now...

on Nov 18

From adamj.eu

Tips for debugging with print() - Adam Johnson

0 0

If you’re embarrassed at debugging with print(), please don’t be - it’s perfectly fine! Many bugs are easily tackled with just a few checks in the right places. As much as I love using a debugger, I often reach for a print() statement first.

on Nov 8

From adamj.eu

Django: How to profile and improve startup time - Adam Johnson

0 0

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.

on Nov 6

From adamj.eu

Django: Introducing Djade, a template formatter - Adam Johnson

0 1

Happy DjangoCon US 2024 to you. Whilst I am not there, I have adopted the spirit of the season and got to work hacking together a new tool.

on Sep 26

From adamj.eu

Git: find when a commit was reverted or reapplied - Adam Johnson

0 1

Git doesn’t store reversion links between commits. It only relies on commit messages to track this information. When you run git revert, the default message includes a line about the reverted commit:

on Sep 19

From adamj.eu

Python: my new uv setup for development - Adam Johnson

0 0

uv is a new, fast Python packaging tool. (I believe it is pronounced as written, “uhv”, rather than spelt out like “you-vee”. This saves a syllable, fitting the tool’s ethos of speed.)

on Sep 18

From adamj.eu

Django: speed up tests slightly by disabling update_last_login - Adam Johnson

0 0

Django’s test client provides two methods to log in a user: login() and force_login(). The latter one is faster because it bypasses the authentication backend, including password hashing, and just sets a session to have the user logged in. Typically, you’d want to use it in setUp() like this:

on Sep 18

From adamj.eu

Python: mock environment variables with unittest - Adam Johnson

0 0

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.

on Sep 18

From adamj.eu

Django: hoist repeated decorator definitions - Adam Johnson

0 0

Django provides us with a rich set of view decorators. In this post, we’ll look at a technique for hoisting repeated use of these decorators to reduce repetition.

on Sep 8

From adamj.eu

Git: avoid reset --hard, use reset --keep instead - Adam Johnson

0 3

When I started learning Git, I found many references covering two ways to undo commits with git reset:

on Sep 5

From adamj.eu

Git: generate statistics with shortlog - Adam Johnson

0 1

git shortlog generate statistics about the commits in a repository, intended to help generate project release notes. By default, it groups commits by author, but it can use other fields too. Use it for a more powerful dissection of a repository than tools like GitHub’s Insights tab.

on Sep 3

From adamj.eu

A Python Script Template with Sub-commands (and Type Hints) - Adam Johnson

0 1

Earlier this week I shared my Python script template. Here’s an extended version with sub-command support, and an example script.

on Sep 1

From adamj.eu

Django: avoid “useless use of .all()” - Adam Johnson

0 0

Here’s a little ORM pet peeve of mine that may deepen your understanding of how QuerySets work.

on Aug 31

From adamj.eu

Django: rotate your secret key, fast or slow - Adam Johnson

0 1

Django’s SECRET_KEY setting is used for cryptographic signing in various places, such as for session storage and password reset tokens. This makes keeping it secure a high priority since an attacker with the key could forge things like password reset tokens.

on Aug 30

From adamj.eu

Python: profile total memory allocated with tracemalloc - Adam Johnson

0 1

tracemalloc is Python’s standard library module for tracking memory allocations. It has many bells and whistles for detailed analysis, allowing you to slice allocations by file and line or compare snapshots. But for simple purposes, displaying the total memory allocated is sufficient, which is...

on Aug 30

From adamj.eu

Django: create sub-commands within a management command - Adam Johnson

0 0

argparse, the standard library module that Django uses for parsing command line options, supports sub-commands. These are pretty neat for providing an expansive API without hundreds of individual commands. Here’s an example of using sub-commands in a Django management command:

on Aug 14

From adamj.eu

How to Safely Pass Data to JavaScript in a Django Template - Adam Johnson

0 0

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!

on Aug 5

From adamj.eu

Introducing django-linear-migrations - Adam Johnson

0 0

If you’ve used Django migrations for a while, you may be familiar with this message:

on Jul 26

From adamj.eu

Git: Rebase stacked branches with --update-refs - Adam Johnson

0 0

When working on a feature, you might split it into several stacked branches, so you can merge each one separately. But updating such branches can be annoying since you have to manage them independently. Git 2.38 (2022-10-15) made this management easier with the new --update-refs option, which...

on Jul 5

From adamj.eu

Django: Test for pending migrations - Adam Johnson

0 0

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

on Jun 26

From adamj.eu

Python: Fail in three characters with 1/0 - Adam Johnson

0 0

Here’s a code snippet I often type:

on Jun 26

From adamj.eu

Python type hints: exhaustiveness checking - Adam Johnson

0 0

Exhaustiveness checking is a very handy type checker feature. It ensures that all possible types of a variable are handled. If your code changes to add another possible type, you can guarantee that exhaustiveness-checked code paths handle the new case.

on Jun 26

From adamj.eu

Python: Import by string with pkgutil.resolve_name() - Adam Johnson

0 0

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):

on Jun 25

From adamj.eu

Django’s release code words, up until 3.2 - Adam Johnson

0 0

It’s now a long-running tradition that each Django release has an associated “code word”. This is used by the release manager in the announcement blog post to describe the list of features coming in the next version.

on Jun 3

From adamj.eu

Python: Mock an inner import - Adam Johnson

0 0

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:

on May 17

From adamj.eu

Python: Show all subclasses of a class - Adam Johnson

0 0

class.__subclasses__() returns a list of the direct subclasses of a given class:

on May 10

From adamj.eu

Django: Introducing django-harlequin, a launcher for Terminal-based SQL IDE Harlequin - Adam Johnson

0 0

Harlequin is a Terminal-based SQL IDE by Ted Conbeer. It is pretty popular, with over 2,500 GitHub Stars and counting. It looks like this:

on May 7

From adamj.eu

Django: An admin extension to prevent state leaking between requests - Adam Johnson

0 0

Here’s a small protection I added to a project a few years ago. I was considering it again since I saw a similar potential bug in a Django middleware.

on Apr 29

From adamj.eu

Python: Diffing unit tests to keep a copy-pasted code in sync - Adam Johnson

0 0

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.

on Apr 26

From adamj.eu

Python: Make line number paths with inspect - Adam Johnson

0 0

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.

on Apr 25

From adamj.eu

Django: Pinpoint upstream changes with Git - Adam Johnson

0 0

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.

on Apr 24

From adamj.eu

Git: Show the first tag containing a commit SHA - Adam Johnson

0 0

Say you have a commit SHA and want to know the first version it was released in. You can use this command:

on Apr 22

From adamj.eu

Boost Your Git DX update out now - Adam Johnson

0 0

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.

on Apr 4

From adamj.eu

Django: Write-up on optimizing the system check framework - Adam Johnson

0 0

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.

on Mar 23

From adamj.eu

Efficient Reloading in Django’s Runserver With Watchman - Adam Johnson

0 0

Update (2022-04-06): pywatchman 1.4.1 does not work with Python 3.10. There is a fix, but unfortunately Facebook have not released it since the issue was reported on 2021-11-02. Check the upstream issue before proceeding on Python 3.10+.

on Mar 21

From adamj.eu

Django: Fuss-free use of Homebrew GDAL/GEOS libraries on macOS - Adam Johnson

0 0

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:

on Mar 20

From adamj.eu

How to optimize PostgreSQL queries from Django using pgMustard - Adam Johnson

0 0

Slow queries happen, and when they do, it can be tough to dissect why they’re slow. This difficulty is compounded by using Django’s ORM, since it generates the SQL for you, so you may have little idea of the actual queries “under the hood”.

on Mar 15

From adamj.eu

Python Type Hints - How to Use typing.cast() - Adam Johnson

0 0

Python’s dynamism means that, although support continues to expand, type hints will never cover every situation. For edge cases we need to use an “escape hatch” to override the type checker.

on Mar 14

From adamj.eu

Django: Join the community on Mastodon - Adam Johnson

0 0

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.

on Feb 10

From adamj.eu

Git: the basics of git bisect - Adam Johnson

0 0

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.

on Jan 30

From adamj.eu

pre-commit: Block files based on name with a custom “fail” hook - Adam Johnson

0 0

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.

on Jan 25

From adamj.eu

Boost Your Django DX update out now - Adam Johnson

0 0

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.

on Jan 23

From adamj.eu

Git: Improve diff generation with diff.algorithm=histogram - Adam Johnson

0 0

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.

on Jan 19

From adamj.eu

PostgreSQL: Full text search with the “websearch” syntax - Adam Johnson

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

on Jan 4

From adamj.eu

Git: Rebase an old branch incrementally - Adam Johnson

0 0

You worked on a sizeable feature branch some time ago but left it to focus on other things. Now you’re ready to pick it up, so you want to rebase it onto your updated main branch. But when you try that, you’re presented with an overwhelming number of merge conflicts.

on Jan 3