Django 1.9 - JSONField in Models

后端 未结 6 601
天命终不由人
天命终不由人 2020-12-30 02:16

I\'m trying to set up a models file in Django 1.9 using the new JSONField. I have found examples using postgres but none with MySql. In the examples with postgres they do

6条回答
  •  -上瘾入骨i
    2020-12-30 02:46

    UPDATE: Django 3.1 now supports JSONField natively for multiple databases: https://docs.djangoproject.com/en/dev/releases/3.1/#jsonfield-for-all-supported-database-backends


    As stated in other answers, Django's native JSONField (as of 1.9 and 1.10) is for PostgreSQL.

    Luckily, MySQL 5.7.8+ comes with a native JSON datatype. You can add it your Django project with the django-mysql package and Django 1.8+

    pip install django-mysql

    from django.db import models
    from django_mysql.models import JSONField
    
    class MyModel(models.Model):
        my_json_field = JSONField()
    

    Read more about the django_mysql JSONField here.

提交回复
热议问题