Skip to main content

A set of utlities for breaking a large Django model down to separate components

Project description

Test status

Break a large model down, transparently

In a project that goes on for several years, models tend to grow and accumulate fields. If you aren’t very disciplined about this, you wake up one day, and find that one of your central tables, one with millions of rows, has 43 columns, including some TextFields. Most of them are not required most of the time, but the default (and common) use is to fetch all of them; also, since this table is queried a lot, the mere fact that it has so many columns makes some of the access slower.

When you realize that, you want to break it into components, such that only a few, most-important columns will participate in the large searches, while further details will be searched and fetched only when needed.

But that is a scary proposition – it might involve subtle code changes, break not just field access but also ORM queries… and this is a central model. The change imagined is open-heart surgery on a large project. Maybe, if we look the other way, it won’t bother us too much…

broken-down-models is here to help you. This is a library which can help you refactor your large model into a set of smaller ones, each with its own database table, while most of your project code remains unchanged.

How?

Django already includes a mechanism where fields for one model are stored in more than one table: Multi Table Inheritance (also known as MTI). That’s what happens when we do “normal” inheritance of models, without specifying anything special in the Meta of either of the models.

Python also supports Multiple Inheritance – one class can have many parent classes. And this also works with Django’s MTI – we can have multiple MTI.

Usually, when we think of a “core” set of attributes with different extensions, and we decide to implement it with MTI, we put this core set in a parent model, and make the extensions subclass it. But in the situation where we try to break down an existing model, this would mean that code which currently uses the large model will have to change, to recognize the new parts.

broken-down-models puts this idea on its head: The extensions become parent models, and the core set is defined in a model which inherits them all. This way, all the fields are still fields of of the model we started with, for all purposes – including not just attribute access, but also ORM queries. For this to really work well, though, some further modifications are required; this is why the library exists, and it is explained in its documentation.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

broken-down-models-0.3.1.tar.gz (16.7 kB view details)

Uploaded Source

Built Distribution

broken_down_models-0.3.1-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

Details for the file broken-down-models-0.3.1.tar.gz.

File metadata

  • Download URL: broken-down-models-0.3.1.tar.gz
  • Upload date:
  • Size: 16.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.10.9 Linux/6.1.0-1-amd64

File hashes

Hashes for broken-down-models-0.3.1.tar.gz
Algorithm Hash digest
SHA256 118e8f9ca1d00c2ab01e68ddd8bccb278b181fc2dd7f9e8cb80e73aa34fa613c
MD5 5f439c655a7793535004fd44cebe6ab6
BLAKE2b-256 b77b08e90c513617ed59955210c883b4f1513308d32da89d72c8670776727038

See more details on using hashes here.

File details

Details for the file broken_down_models-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for broken_down_models-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f44ebf64c44eb4e12bfe05e677f3a0f5194c00dc4b9957a44c12f05f8f9fbfd6
MD5 ae13048dfffef6543de0307df0b73261
BLAKE2b-256 80b6a60c852ddaf271ba89c2133661dee26919f22d3af9e54e8a172c6922990d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page