0

I have a Django project and such array in it:

{(0,0):{'a':True,'b':False ... },(0,1):{'a':True,'b':True ... },(1,1):{'a':True,'b':False ... }... }

I need to store this array in PostgreSQL database. I thought the ArrayField could help, but I just have no idea how to declare such array. I've read https://docs.djangoproject.com/en/1.8/ref/contrib/postgres/fields/
but there was not my situation. Could you show me an example?

I'm using Django 1.8

1
  • Yes you can JSON fields, also make sure that your postgres version should be 9.4 or higher, that only supports json field. Commented Apr 21, 2017 at 4:38

2 Answers 2

1

JSON field should be perfect for you. https://docs.djangoproject.com/en/dev/ref/contrib/postgres/fields/#querying-jsonfield

from django.contrib.postgres.fields import JSONField
from django.db import models

class ModelName(models.Model):
    json_data = JSONField()

Now to save the data,

ModelName.objects.create(json_data={(0,0):{'a':True,'b':False ... },(0,1):{'a':True,'b':True ... },(1,1):{'a':True,'b':False ... }... })
Sign up to request clarification or add additional context in comments.

Comments

0

PostgreSQL has two types you may want to check out,

Both have overlapping functionality. JSON is part of the SQL 2016 spec though; hstore while having some advantages on JSONB is a PostgreSQL thing

1 Comment

Looks like i should use newer version of Django to solve my problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.