Knowledgebase

Working With the Django ORM Print

  • backenddevelopment, backend, migration, database, performance, guide, howto, solution
  • 0

Data access.

WHAT IT PROVIDES

Models defining tables, with relationships and constraints.

WHAT MIGRATIONS DO

Generate and apply schema changes from model definitions.

WHAT TO ALWAYS DO

Review generated migrations before applying them.

WHY

Generated changes occasionally do something destructive you did not intend.

WHAT THE COMMONEST PERFORMANCE FAULT IS

Accessing a related object for each item in a loop, producing a query per item.

WHAT SOLVES IT

Selecting or prefetching related data with the original query.

WHICH TO USE

One for single-valued relationships, joining in the same query The other for multi-valued relationships, using a second query

WHAT TO MONITOR

Query count per request, during development.

WHAT ELSE TO BE CAREFUL WITH

Loading entire tables into memory Querying without indexes on filtered fields Counting by loading all records

WHAT TO USE INSTEAD OF THAT LAST

The database's own counting.

WHAT TO KNOW ABOUT QUERYSETS

They are lazy, evaluated only when used.

WHY THAT MATTERS

The place a query executes may not be where it was written.

WHAT TO USE FOR MONEY

Exact decimal fields.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot