Questions tagged [django-jsonfield]
162 questions
4
votes
1 answer
Django Models: Add validation to custom field
TLDR;
How do I provide custom, model level, field validation which is encapsulated inside the field class?
The Rest
I am playing with the two JSONField implementations, (first, second). I am using a combination of Django and Django REST framework…
user2913694
3
votes
2 answers
Django JsonField filter by two fields
class Blog:
values = JSONField(blank=True, default=list)
[
{
"id": 1,
"value": "31"
},
{
"id": 2,
"value": "Hello"
},
...
]
I need to get all objects where the id is 1 and value of that field is greater than 31.
I have…

Yelibay Nuptebek
- 33
- 4
3
votes
2 answers
How to filter by the last element of a json array field in Django
I have a model MyModel with a JSONField called data. This json field contains an array of objects like:
[
{
"date": "2021-01-01",
"amount": 120
},
{
"date": "2021-01-02",
"amount": 150
}
]
I would…

nbeuchat
- 6,575
- 5
- 36
- 50
3
votes
2 answers
Django queryset filter empty JSONField
I would like to filter a JSONField my_fieldwhere the JSon is not empty. In the documentation there are has_keyand has_any_key methods but I don't know the possible keys in advance. In SQL it should be something like:
select * from my_model where…

Othman
- 332
- 5
- 19
3
votes
0 answers
how to use a column value as a key in json field with django orm and postgres?
I need to find how to build a query with django ORM
I have two tables with a relation one to many.
table A
id = integer autonumeric
data = postgres json
...
table B
id = integer autonumeric
id_A = table A id
name = string
t_name = string like slug…
3
votes
0 answers
How to count the number of keys in a django jsonfield
I would like to make a queryset filter to identify all model instance that have a given number of keys in a jsonfield of the model.
I have tried to create a custom lookup (#1) to extra the keys in the json field,
and would like to aggregate those…

user2357068
- 111
- 1
- 4
3
votes
3 answers
Filter list items within a Django JSONField
Here is a simplified version of a Django model I'm working on.
class Course(models.Model):
infos = JSONField()
The infos JSONField looks like this :
infos = {
category: "Maths",
students: [
{
name: "Alice",
…

Thomas Milan
- 289
- 1
- 5
- 14
3
votes
1 answer
Django: filter JSONField with multiple nested arrays
I have a JSONField, called metadata on my model in Django.
The data in that fields might looks something like this
{
"vis": {
"plots": [
// some objects here
{
"id": 1,
"x": "foo",
…

freethebees
- 957
- 10
- 24
3
votes
2 answers
In django queryset filter, how to check if at least one key in json field has non empty value?
class Library(models.model):
book = JSONField(default=[], blank=True, null=True)
possible structure of 'book' is {'title':'' ,'no_of_pages': '', 'author_name': '', 'color': '', edition: ''}
I am trying to write a django query that returns only…

priyankarani
- 91
- 1
- 2
- 5
3
votes
1 answer
Django jsonfields ValidationError: [u'Enter valid JSON'] upon upgrade from 1.7 to 1.9
I'm upgrading django from 1.7 to 1.9 by means of replacing packages, installed in system (Debian 8) via apt-get with packages, installed via pip.
So, I installed jsonfield package from pip instead of apt's good old python-django-jsonfield and tried…

Boris Burkov
- 13,420
- 17
- 74
- 109
2
votes
2 answers
Django ListView count all keys in a JSON variable
I would like to return in my Link.html the number of links contained in allLinks JSON variable.
So far I guess I misunderstand the use of get_context_data() and how to pass in context['CountLink'] the total count of links for each Post.
With the…

fransua
- 501
- 2
- 18
2
votes
2 answers
writing Umlaute to mariadb causes non-UTF-8-encoding
I am using mariadb with Server charset: UTF-8 Unicode (utf8mb4) and python 3.7.3 and for some reason beyond me, a CSV file read in and written to the database is saved in some weird encoding:
models.py:
class Product(models.Model)
data =…

xtlc
- 1,070
- 1
- 15
- 41
2
votes
1 answer
Django JSONField data with integer keys
I want to save a model instance with JSON dict with integer keys, like {2: 3}.
But after saving my dict turns into {"2": 3}. Is there any way to save integer keys into JSON?
class MyModel(models.Model):
data = JSONField("data")
record =…

Yevhen Bondar
- 4,357
- 1
- 11
- 31
2
votes
2 answers
Django JSONField complex query ... practical example of querying complex nested data structure
I have inherited the following JSONField data structure:
[
{
"name": "Firstname",
"show": {
"value": true
},
"type": "text",
"uuid": "55668e45-07d1-404e-bf65-f6a3cacfaa97",
"label": {
"for": "Firstname",
…

Micheal J. Roberts
- 3,735
- 4
- 37
- 76
2
votes
1 answer
How to Django ORM update() multiple values nested inside a JSONField?
I have a Django JSONField on PostgreSQL which contains a dictionary, and I would like to use the queryset.update() to bulk update several keys with values. I know how to do it for one value in a JSONField (and for multiple fields in general):
from…

Shaheed Haque
- 644
- 5
- 14