Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 1.9 - JSONField in Models

Tags:

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 a

from django.contrib.postgres.fields import JSONField 

How do I go about importing it for MySql? Thanks

like image 802
Ravash Jalil Avatar asked May 03 '16 14:05

Ravash Jalil


1 Answers

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.

like image 199
aboutaaron Avatar answered Oct 27 '22 05:10

aboutaaron