Questions tagged [django-jsonfield]
162 questions
7
votes
3 answers
Django db error: could not identify an equality operator for type json when trying to annotate a model with jsonfield
I'm working in Django 1.5.4 and PostgreSQL 9.3, using django-jsonfield for JSONField.
Following query throws db error (could not identify an equality operator for type json):
ModelWithJsonField.objects.annotate(count=Count('field_to_count_by'))
The…

Neara
- 3,693
- 7
- 29
- 40
6
votes
3 answers
add user friendly json editor to django admin
I have a django app, using also rest_framework, and a model Product with field of type JSONField. so data is stored as JSON in Postgres, Now I want to provide the admin with a nice user friendly way on how he can change the json field (names/keys…

anyavacy
- 1,618
- 5
- 21
- 43
6
votes
3 answers
Django JSONField to have particular keys
My class looks like this:
class Foo(models.Model):
known_by = JSONField()
My data looks like this
{ "known_by" : [
{'by':'name1', 'value':['def']},
{'by':'name2', 'value':['bar']}
]
}
Is there any…

suprita shankar
- 1,554
- 2
- 16
- 47
6
votes
2 answers
Django admin page: Customize dictionary (JSONField) of IDs through multiple Models selects instead of raw text
I have a model in which one of its fields is a postgres.fields.JSONField.
The Json that is going to be stored there is a variable dictionary of IDs referencing other items (possible relations/attributes) in the database.
Allow me to be more…

Savir
- 17,568
- 15
- 82
- 136
5
votes
2 answers
How to annotate sum over Django JSONField (Array of objects) data?
I have models sth like this
# models.py
class MyModel( models.Model ):
orders = models.JsonField(null= True, blank=True, default=list)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
I stored json data in this structure.
[
…

Bimal Prasad Pandey
- 561
- 3
- 13
5
votes
1 answer
Django JSONField does not save 0 value
I have a JSONField in my model, when I try to save 0 as value in json in django admin page, it saves value as null. How can I save zero as value?
Django version: 2.1.7
Django admin page:
my JSONField:
lecturer_scores = JSONField(
…

Ali Soltani
- 589
- 1
- 7
- 21
5
votes
1 answer
Django JsonField to forms
I have searched for a couple days to try to find a DRY way to create dynamic forms from a JsonField using django's built in validation and form rendering. I have yet to find a solution that will incorporate django's validation and rendering so I…

Dmckim
- 63
- 6
5
votes
1 answer
Query django JSONField for nested value in list of nested objects
I have Sentence model which metadata as JSONField
One sample row is
Sentence.objects.filter(id=6753315).values('id', 'metadata')[0]
{'id': 6753315,
'metadata': [{'filters': [{'id': None, 'level_name': 'Brand Hierarchy'},
…

Alok
- 7,734
- 8
- 55
- 100
5
votes
2 answers
JSONField getting saved as string django
I have a django model like below:
from jsonfield import JSONField
class SCUser(User):
address = JSONField(blank=True,null=True)
When I save a json in this address it gets saved as string.
Here is a code snippet:
appuser.address =…

Aarohi Kulkarni
- 427
- 2
- 5
- 19
5
votes
1 answer
DjangoModelFactory JsonField returning Unicode data instead of dictionary data
I am trying to set a field of type JSONField using factoryboy DjangoModelFactory. Here is the code:
class SubmittedAnswer(models.Model):
data = JSONField(default={})
is_rule_check_passed = models.NullBooleanField()
class…

Shubham
- 3,071
- 3
- 29
- 46
4
votes
0 answers
How to Django ORM update() a value nested inside a JSONField with a numeric value?
I have a Django JSONField on PostgreSQL which contains a dictionary, and I would like to use the queryset.update() to bulk update one (eventually, several) keys with a numeric (eventually, computed) values. I see there is discussion about adding…

Shaheed Haque
- 644
- 5
- 14
4
votes
0 answers
json field in django-haystack with elasticsearch engine
How can I store json in elasticsearch by django-haystack fields?
At the moment I am using following trick:
# search_indexes.py
class BlogPostIndex(indexes.SearchIndex, indexes.Indexable):
author = indexes.CharField()
@staticmethod
def…

ehsan ahmadi
- 365
- 2
- 13
4
votes
1 answer
How to correctly deserialize datetime objects stored in JSONField with DjangoJSONEncoder?
I'm storing datetime objects in a JSONField with Django 2.1 and a PostgreSQL, but I can't find a way to correctly deserialize them when querying database :
I've tried to use DjangoJSONEncoder which works correctly for serialization.
But…

Timothé Delion
- 1,310
- 12
- 17
4
votes
1 answer
Why do different Django Models with JSONFields have the same values?
I've got a model with a JSONField (a Postgres only field):
models.py:
from django.db import models
from django.contrib.postgres.fields import JSONField
class Mod(models.Model):
data = JSONField(default={ 'name':'Model' })
So I create 2 models…

Jeremy S.
- 1,086
- 8
- 18
4
votes
1 answer
Fake JSONField using Factory Boy
I have a field in my model with JSONField Type(MYSQL Implementation). I want to fake the data for this field using FactoryBoy Faker.
How can I achieve this?

Anshul Sharma
- 327
- 4
- 14