The Django web framework includes a default object-relational mapping layer (ORM) that can be used to interact with application data from various relational databases such as SQLite, PostgreSQL and MySQL.
The Django ORM has evolved over the past dozen years since it was created make sure to not only read up on the latest tutorials but also learn about newer optimizations, such as prefetch_related and select_related, that have been added throughout the project's history.
Django models, encapsulation and data integrity is a detailed article by Tom Christie on encapsulating Django models for data integrity.
Django Debug Toolbar is a powerful Django ORM database query inspection tool. Highly recommended during development to ensure you're writing reasonable query code. Django Silk is another inspection tool and has capabilities to do more than just SQL inspection.
Django QuerySet Examples (with SQL code included) teaches how QuerySets work and shows the corresponding SQL code behind the Python code you write to use the ORM.
Making a specific Django app faster is a Django performance blog post with some tips on measuring performance and optimizing based on the measured results.
Why I Hate the Django ORM is Alex Gaynor's overview of the bad designs decisions, some of which he made, while building the Django ORM.
Going Beyond Django ORM with Postgres is specific to using PostgreSQL with Django.
Migrating a Django app from MySQL to PostgreSQL is a quick look at how to move from MySQL to PostgreSQL. However, my guess is that any Django app that's been running for awhile on one relational database will require a lot more work to port over to another backend even with the power of the ORM.
Adding basic search to your Django site shows how to write generic queries that'll allow you to provide site search via the Django ORM without relying on another tool like ElasticSearch. This is great for small sites before you scale them up with a more robust search engine.
The Django ORM Cookbook provides code recipes for various ways to use the Django ORM to insert and query data.
How to use Django's Proxy Models is a solid post on a Django ORM concept that doesn't frequently get a lot of love or explanation.
Tightening Django Admin Logins shows you how to log authentication failures, create an IP addresses white list and combine fail2ban with the authentication failures list.
How to Turn Django Admin Into a Lightweight Dashboard and How to Use Grouping Sets in Django are two great posts on how to add custom features to the Django Admin as well as optimize with more advanced SQL when the first attempt at the queries get slow due to larger amounts of data.
Sorting querysets with NULLs in Django shows what to do if you're struggling with the common issue of sorting columns that contain NULL values.
Best Practices working with Django models in Python
has a ton of great advice on proper model naming conventions, quirks to
ForeignKey field relationships, handling IDs and many other
edge cases that come up when frequently working with Django's ORM.
Merging Django ORM with SQLAlchemy for Easier Data Analysis provides rationale for using the SQLAlchemy ORM instead of Django's default ORM in some situations.
Working with huge data sets in Django
explains how to slice the data you retrieve by query into pages and then
prefetch_related on a subset of the data rather than your whole
Solving performance problems in the Django ORM
gives a slew of great code snippets to use with
you can discover issues such as unexpected extra queries and problematic
Django ORM optimization story on selecting the least possible goes through one developer's Django ORM code refactoring to optimize the performance and results of a single query.
Fixing your Django async job - database integration is a great article on how to properly integrate the RQ task queue with a Django backend.
Django migrations were added in version 1.7. Django projects prior to 1.7 used the South project, which is now deprecated and merged into Django. Migrations can be tricky to wrap your head around as you're getting started with the overall framework but the following resources should get you past the initial hurdles.
Django Migrations - a Primer takes you through the new migrations system integrated in the Django core as of Django 1.7, looking specifically at a solid workflow that you can use for creating and applying migrations.
Django 1.7: Database Migrations Done Right explains why South was not directly integrated into Django, how migrations are built and shows how backwards migrations work.
Executing custom SQL in Django migrations examines how you can hook in straight SQL that will run during a Django migration.
Squashing and optimizing migrations in Django shows a simple example with code for how to use the migrations integrated into Django 1.7.
Supporting both Django 1.7 and South explains the difficulty of supporting Django 1.7 and maintaining South migrations for Django 1.6 then goes into how it can be done.
Writing unit tests for Django migrations contains a ton of awesome code examples for testing your migrations to ensure data migrations work well throughout the lifecycle of your Django project.
Strategies for reducing memory usage in Django migrations shows the large memory usage problem that often occurs with Django migrations at scale and what you can do to mitigate the issue.
How to Create Django Data Migrations has a straightforward blog ORM modeling example to show how to perform data migration.
Keeping data integrity with Django migrations
shows two table modification scenarios, one where a column needs to be
added to an existing table, and another where a
Many-to-Many field needs
to be converted to a standard
ForeignKey column while retaining all
of the data.
Double-checked locking with Django ORM shows how you can implement a double-checking locking pattern in the Django ORM with PostgreSQL, which is useful when you want to prevent multiple processes from accessing the same data at the same time.
Using Django Check Constraints for the Sum of Percentage Fields
shows how you can combine several
fields with a checking constraint and a web form that ensures
all of the fields sum up to a precise amount, such as 100%.
Software errors are inevitable. Chaos is not. Try Sentry for free.