Speed up Django & Postgres with simple JSON field

眉间皱痕 提交于 2019-12-06 08:50:18

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!