Make Django Tests Always Rebuild the Database if It Exists
If you use Django’s test runner, you’ll probably have encountered this message:
(All tags.)
If you use Django’s test runner, you’ll probably have encountered this message:
Bruno Oliveira, known for his work on the pytest project, tweeted this thread last July:
pytest is quickly becoming the “standard” Python testing framework. However it can be overwhelming to new users.
Several large Django applications that I’ve worked on ended up with memory leaks at some point. The Python processes slowly increased their memory consumption until crashing. Not fun. Even with automatic restart of the process, there was still some downtime.
Django 3.0 alpha 1 came out this week. It introduces ASGI support thanks to lots of hard work by Andrew Godwin.
datetime modulePython’s datetime module risks a whole bunch of name confusion:
A few weeks ago I had the pleasure of talking over the internet with Will Vincent and Carlton Gibson about lots of Django-related topics. They somewhat informed me it was being recorded for a podcast.
On several Django projects I’ve worked on, there has been a requirement for performing database modifications beyond Django migrations. For example:
In my blog post and DjangoCon Europe talk earlier this year How to Score A+ for Security Headers on Your Django Website, I covered that Feature-Policy was a “bonus header”. In a recent update, Scott Helme wrote that an A+ on SecurityHeaders.com now requires Feature-Policy. Also it no longer requires X-Xss-Protection (though it’s still a good idea).
This is a story about how I sped up a client’s Django test suite to be three times faster, through swapping the test case class in use.
One thing I often ask for in code review is conversion of tuples to lists. For example, imagine we had this Django admin class:
Every time I pick up the Python job queue Celery after not using it for a while, I find I’ve forgotten exactly how RabbitMQ works. I find the Advanced Message Queuing Protocol (AMQP) concepts drop out of my head pretty quickly: Exchanges, Routing Keys, Bindings, Queues, Virtual Hosts…
If the title worried you about my mental state, you can relax. It’s about the Python code formatter Black!
My library apig-wsgi bridges between AWS API Gateway’s JSON format for HTTP requests and Python WSGI applications. Recently Théophile Chevalier opened an issue requesting the library add an extra WSGI environ variable. I closed it by pointing out that it’s not much code to add a custom WSGI middleware to do so (plus the exact key is a bit out of scope for the library).
I discovered GIFCities recently. It’s a project by the Internet Archive for browsing GIFs extracted from their Geocities archive.
DjangoCon Europe 2019 was great fun. I loved meeting old and new friends, giving my talk and workshop, and learning in the other talks.
Code coverage is a simple tool for checking which lines of your application code are run by your test suite. 100% coverage is a laudable goal, as it means every line is run at least once.
A discussion recently came up on django-developers mailing list about how to build the SQL query CREATE TABLE ... AS SELECT (CTAS) with Django’s ORM. This statement and its cousin INSERT ... SELECT are useful for re-shaping data inside your database, using SELECT queries.
This is a blog post version of the talk I gave at DjangoCon Europe 2019 on the 10th April.
A lot of people pick Flask over Django because they believe it is simpler to start with. Indeed, the Flask front page includes an 8 line “hello world” application, while the Django default project has 172 lines in 5 files, and still doesn’t say “hello world”! (It does show you a welcome rocket and have a full admin interface though, both are pretty fun).
I was at PyCon Namibia in Windhoek from the 19th to 21st of February, and had an amazing time! PyCon Namibia is one of the longest running PyCons in Africa, this being its fifth edition in as many years.
I quite often get asked for resources on where to learn Django. Here’s a list of the places I know!
I realized I’ve posted the 2017 and 2018 editions of the London Django Meetup December quizzes on my blog, but forgot to post the first one in 2016. So here it is reproduced below, if you’d like to play at home or scroll through to the answers to pick up on some more Django trivia.
Reading the Django 2.2 alpha announcement, the phrase “salmagundi of new features” stood out to me. I had to look up “salmagundi” in Wiktionary, where it is defined as:
On Monday evening I gave a quiz at the December London Django Meetup Group for the third year running - that makes it a tradition! Here it is so you can follow it at home - answers are at the bottom, no cheating.