My Favorite Django Resources

I’ve been working with Django full time since roughly 2016. 8 years feels like an eternity in the software world. In my opinion, no other framework compares to Django in terms of off-the-shelf solutions In Python land that is, I know Rails is also a great experience. If you have a problem, there’s a Django exmaple in a book, youtube video, or blog post for it. Where does one even begin to look?

Over my tenure as a Django addict, I’ve built up a small library of resources. Whether you are a Django beginner or expert, there’s something in store for you.

1. Favorite Book

Hands down, Two Scoops of Django by Daniel and Audrey Greenfeld. This book has been written for multiple versions of Django. The earliest was written for Django 1.5, which was released in 2013. The latest is written for Django 3.x. I reference my copy of Two Scoops all the time. Daniel and Audrey have also authored A Wedge of Django. I haven’t read this book, but have seen it recommended by others on my team.

2. Documentation

Don’t discount reading the official docs. Save a bookmark to the documentation table of contents and use ctrl+f. I can’t tell you how many times I’ve reread about data migrations or form fields. Spend some time in there, get comfortable.

Aside from the official docs, the classy docs also provide great documentation:

These docs are great for understanding the interfaces exposed in various class-based parts of Django. As a beginner, it can be difficult to figure this out on your own. It also annotates the methods by inheritance. This can be useful for understanding how everything pieces together.

While not stictly documentation, the Django in Depth video by James Bennett is insightful. James has been using Django longer than I’ve been programming. Django’s fundamentals have not changed significantly over the years, the video content is still relevant. If you are a “from the bottom up” kind of thinker, this one is for you.

3. Styleguides

Maintaining consistency in larger projects is a heavy task. Thankfully, others have put significant energy into documenting this. I discovered these through David Winterbottom, who created django-oscar. One of my favorite notes is encapsulating model mutation. Tom Christie, author of Django REST Framework, pubished Django models, encapsulation and data integrity for further reading on the subject.

4. Cookbooks

You can bet your bottom dollar every Django project will be using the Admin and ORM. There are two respective resources that serve as great resources for understanding their practical uses:

  1. Django Admin Cookbook
  2. Django ORM Cookbook

These are great for skimming to understand what some of the possibilies are. Or, if you are stuck with with a specific ORM/Admin question. The Admin and ORM are expansive subjects with lots of ground to cover. Having bite size example can help you digest at your own rate.

5. Finding Packages

Django Packages is still my first go to for finding packages. It’s comprehensive and covers most everything you need. Chances are someone else has already ran into, and solved, the same issue you have. If you still cannot find anything, go to the packaging source at pypi. Still no package? Search for code on Github. There’s a package for almost everything.

6. CMS Framework

Coming from Wordpress and looking for a Python equivalent? Well, Wagtail’s got your back. It’s built on top of Django with all the goodies you would expect from a CMS. And, if you are interested in paid learning experience, has you covered. I thoroughly enjoyed using wagtail and would highly recommend it.

7. Django Hot Takes

What post would not be complete without some hot takes?

7.1 Prefer Function Based Views

Avoid class based views as much as possible and read Django Views — The Right Way. Another old, but great, video on the subject of classes is Stop Writing Classes. Class Based Views are, initially, a great way to hit the ground running. They handle most of the gruntwork for you, which results is less lines of code. Less lines = good, right?

Abstractions are a powerful tool for helping us move quickly without requiring us to understand the entire machine. The problem arises when those abstractions don’t align with our real world requirements. You begin to add mixins, overwrite methods, and call super() in an attempt to squeeze out the correct functionality. Which method are you supposed to correctly overwrite? Probably the one that’s easiest to copy/paste. In the end, it works, but the cost is understanding.

Understanding the legacy code as well as the python MRO is no trivial task. Understanding the sequential execution of function is much easier. Code that is easier to reason about is easier to maintain and test. You will never be able to completely avoid classes, it’s how third party packages, like django rest framework, expose their views. You can reduce the surface area of class based views. It will cause you less pain in the long run.

7.2 Simplify Your Django Apps

When you first start with Django, you will likely build many small apps for new features. It’s a logical first step. After a while each app will eventually import and bleed into each other. I’m much more in favor of ditching apps altogether for your own application. How to structure Django Projects is similar to how I organize django projects. The main idea being separate directories for:

  • Models
  • Business Logic
  • Presentation (views/forms)

I also like to add another directory for anything calling third party services. This captures all the calls in one place and exposes them to the rest of the application. Updating the version of a libary? Changing your email service? All the calls are neatly tucked in one place.

Having the business logic separate from all things django makes structuring and testing easier. The functions in these modules can deal exclusively with python primatives. Which means you do not need to mock the world in order to test business logic. Business logic changes frequently and is, arguably, the most important to get right. Make the process easier.

7.3 Avoid Service Objects

Figuring out where to put business logic in Django can be complicated. Another solution, besides fat models, that pops up from time to time is Django Service Objects. Sometimes also referred to as Django Service Layers. I’ve gone done this path and found the results a bit…clunky. I would recommend reading Against service layers in Django by James Bennett. Custom Manager and Queryset classes are much more easier to maintain.

8. Conclusion

That’s about it. It never hurts to have a good python cheatsheet if you’re just picking up the basics. Shoutout to my dear friends Ashia Zawaduk, Bryan Hyshka, and Kalob Taulien. Without which, my Django adventures and learnings would not have been half as fufilling. Good luck on your own Django Adventures!