Speed up Django & Postgres with simple JSON field

2019-06-02 01:09发布

问题:

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.

To do something like this:

class Very ComplexModel(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!

回答1:

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



回答2:

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