6

Can anyone tell me how to define a json data type to a particular field in models.

I have tried this

from django.db import models
import jsonfield

class Test(models.Model):
    data = jsonfield.JSONField()

but when I say python manage.py sqlall xyz its taking the data field as text

BEGIN;
CREATE TABLE "xyz_test" (
   "data" text NOT NULL
)
; 

I still tried to insert json data into that field but its giving error as :

ERROR:  value too long for type character varying(15)

someone please help. Thanks in Advance.

tmthydvnprt
  • 10,398
  • 8
  • 52
  • 72
G-Coder
  • 87
  • 2
  • 3
  • 9
  • 2
    Well, in the background [JSONField actually *is* a TextField](https://github.com/bradjasper/django-jsonfield/blob/master/jsonfield/fields.py#L142), so that's not a problem, that's the expected behavior. However, the 15-chars limit is a tad weird - can you share the value you were trying to save? – yuvi Sep 17 '14 at 07:22
  • @yuvi this is the value - {'name':'xyz', 'email':'xyz@gmail.com', 'phone':'0123456789'} – G-Coder Sep 17 '14 at 07:29
  • I just recreated your model and was able to save that exact value to a jsonfield. Are you sure it's the jsonfield causing that error and not some different field that has a 15 chars limit? – yuvi Sep 17 '14 at 07:39
  • ya its for that text field, anyways can you please post the procedure that you followed or syntax. – G-Coder Sep 17 '14 at 07:52
  • I copied your exact code, and it worked (both when I passed the json as a python dictionary and as a JSON string). Please post your code that produces the error below – yuvi Sep 17 '14 at 08:04
  • 1
    appologies.!! error was from other field. got it buddy. – G-Coder Sep 17 '14 at 08:13
  • +1 for you time and help. – G-Coder Sep 17 '14 at 08:14
  • Sure thing. Added a proper answer below – yuvi Sep 17 '14 at 08:19

4 Answers4

7

In the background JSONField actually is a TextField, so that output from sqlall is not a problem, that's the expected behavior.

Further, I recreated your model and it worked just fine, both when entering the value as a string and as a python dictionary, no character limitation whatsoever. So my best guess it that the issue is a completely unrelated field that does have a 15 chars limit.

Bharathi
  • 121
  • 1
  • 8
yuvi
  • 18,155
  • 8
  • 56
  • 93
3

A JSONField data type has been added to Django for certain databases to improve handling of JSON. More info is available in this post: Django 1.9 - JSONField in Models

Ben
  • 4,798
  • 3
  • 21
  • 35
3

You can try this:

from django.contrib.postgres.fields import JSONField

class Test(models.Model):
    data = models.JSONField()
funie200
  • 3,688
  • 5
  • 21
  • 34
  • why are we importing `from django.contrib.postgres.fields import JSONField` if at the end we have to use `models.JSONField()`? – Hammad Mar 30 '22 at 08:47
  • 1
    "django.contrib.postgres.fields.JSONField is removed except for " "support in historical migrations." Use django.db.models.JSONField instead. – Elcast May 17 '22 at 19:16
0

First install django-jsonfield pypi package:

pip install django-jsonfield

Then simply use it:

from jsonfield import JSONField

class MyModel(models.Model):
    custom_field = JSONField(
        default=dict
    )
Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150