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.5.0.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded Python 3

File details

Details for the file broken_down_models-0.5.0.tar.gz.

File metadata

  • Download URL: broken_down_models-0.5.0.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.13.3 Linux/6.12.27-amd64

File hashes

Hashes for broken_down_models-0.5.0.tar.gz
Algorithm Hash digest
SHA256 7dbca017f72e3d7a07d5c1f6a02936dc7b15cf1d7bb506b7e15fe16b4a9de1dd
MD5 fe31b041ca311e252a858e3e7f85dc10
BLAKE2b-256 0414d9a58099589b39b24b9000aea0baa57562bf9b8053635c65727dc0838456

See more details on using hashes here.

File details

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

File metadata

  • Download URL: broken_down_models-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 16.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.13.3 Linux/6.12.27-amd64

File hashes

Hashes for broken_down_models-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 66a8f7b7cbd090897600a1ac9c9fd663454aa44a9df712f327924366d9ad88c0
MD5 3bee41c2c4a7ec7ef42e9d83ff02d827
BLAKE2b-256 24d496396d7f4437575e20b744f80b238f0ef3b2a510673ba9977d57d8309745

See more details on using hashes here.

Supported by

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