I have a very very complex model with lots of related models by FK and M2M which are also have lots of relations, etc.
So, rendering a list of such objects is a very expensive SQL operation, and i want to optimise it. (select_related and prefetch_related help, but a little)
I have maybe a very stupid but very simple idea - define save method, that will serialize all object's data to a field stores JSON
To do something like this:
class VeryComplexModel(models.Model):
# some_field
# some_field
# ...
json = models.TextField()
def save(self):
json = serialize(self)
in views.py:
complexModels = ComplexModel.objects.get_values(json)
And in template:
{% for m in complexModels %}
{{ m.some_field }}
{{ m.some_field.some_fields.some_field }}
{% endif %}
Is it a bad idea? Maybe it is a good idea in general, but I should use more suitable stuff like special JSON field or something?
Big thanx for advices!