Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speed up Django & Postgres with simple JSON field

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!

like image 371
MaxCore Avatar asked May 18 '26 09:05

MaxCore


2 Answers

Django supports JSONField for PostgreSQL, here is the example

from django.contrib.postgres.fields import JSONField
from django.db import models

class Dog(models.Model):
    name = models.CharField(max_length=200)
    data = JSONField()

    def __str__(self):  # __unicode__ on Python 2
        return self.name

also you can read more about it on this link https://docs.djangoproject.com/en/dev/ref/contrib/postgres/fields/#jsonfield

also you can try out HStoreField in postgresql, HStoreField is faster than the JSONField, for using HSTORE you need to enable Hstore extension in Postgresql

postgres_prompt=> create extension hstore;

in your migration file you need to add this

from django.contrib.postgres.operations import HStoreExtension

class Migration(migrations.Migration):
    ...

    operations = [
        HStoreExtension(),
        ...
    ]

here is an example of using Hstore in your models:

from django.contrib.postgres.fields import HStoreField
from django.db import models

class Dog(models.Model):
    name = models.CharField(max_length=200)
    data = HStoreField()

    def __str__(self):  # __unicode__ on Python 2
        return self.name

to know more about this go to the l: https://docs.djangoproject.com/en/1.9/ref/contrib/postgres/fields/#hstorefield

like image 157
Chitrank Dixit Avatar answered May 21 '26 01:05

Chitrank Dixit


Django supports JSONField for PostgreSQL, take a look: PostgreSQL specific model fields

like image 20
ezdookie Avatar answered May 21 '26 01:05

ezdookie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!