Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django model with hundreds of fields

Tags:

python

django

I have a model with hundreds of properties. The properties can be of different types (integer, strings, uploaded files, ...). I would like to implement this complex model step by step, starting with the most important properties. I can think of two options:

  1. Define the properties as regular model fields
  2. Define a separate model to hold each property separately, and link it to the main model with a ForeignKey

I have not found any suggestions on how to handle models with lots of properties with django. What are the advantages / drawbacks of both approaches?

like image 966
blueFast Avatar asked Sep 05 '25 07:09

blueFast


2 Answers

You definitely should not define your properties as ForeignKeys. Every time you need a full model, your database server will have to make hundreds of JOINs, therefore ruining your performance.

If your properties are needed almost every time you access the model, you should keep them in the same model. If not, you could make a separate Properties model and link it to your original model via OneToOneField.

I personally had such an experience. We had to build a hotel recomendation engine, and we were using Drupal back then. And as Drupal stores every custom property in a separate MySQL table, we quickly realised we should switch the framework, because every single query crashed our production servers (20+ JOINs are a deadly thing to MySQL). BTW, we ended up using a custom solution based on ElasticSearch, which handles hundreds of fields just fine.

Update: If you're lucky enough to be using a recent version of PostgreSQL, you could leverage the JSONField storage to pack all your fields to a single model field. Note, though, that you'll have to implement a validation scheme by yourself.

like image 109
Alex Morozov Avatar answered Sep 07 '25 19:09

Alex Morozov


customer requirement.

First off, I feel your pain and wish you the best! I wish to reiterate if this wasn't the case that you should be first looking to change this as there should never be any need for hundreds of properties on a single object, it normally shows a need for an array, inheritance, or separate classes etc..


Going forward, you're going to need to heavily make use of values and values_list to only return the properties that you actually need from the database since performance will be severely crippled from this.

Since you can't do anything with the model, you should try to address your performance issues from the design side of things. The single responsibility principle should feature heavily in your website which will mean you'll only ever have a few values needed to be returned from the model. This way it really won't make much difference what option you choose since what is returned will be very limited.

Filter where you can, and use ordering sparingly.

like image 38
Sayse Avatar answered Sep 07 '25 20:09

Sayse