Questions tagged [django-jsonfield]

162 questions
2
votes
1 answer

Get Django objects JSONField that contains any item of list in its specific value

I have a JSONField in Django model like this: #Object1 JSONField: {"Fruits": ["banana", "cherry", "apple", "strawberry"], "Vegetables": ["broccoli", "cucumber", "eggplant"]} #Object2 JSONField: {"Fruits": ["fig", "coconut", "pear"], "Vegetables":…
user15821905
2
votes
1 answer

how can i filter field django jsonfield that is datetime?

I want to filter base on time field in jsonfield,but this field store as string and i can not use this query: Notification.objects.filter(Q(data__payload__request_visit_time__gte=timezone.now()))
arezoo
  • 328
  • 3
  • 14
2
votes
4 answers

AttributeError: module 'django.db.models' has no attribute 'JSONField'

Following Django 3.1 guidelines for JSONField for all supported database backends… https://docs.djangoproject.com/en/3.1/releases/3.1/#jsonfield-for-all-supported-database-backends WARNINGS: users.Search.input: (fields.W904)…
JV conseil
  • 328
  • 5
  • 11
2
votes
2 answers

Query Django JSONFields that are a list of dictionaries

Given a Django JSONField that is structured as a list of dictionaries: # JSONField "materials" on MyModel: [ {"some_id": 123, "someprop": "foo"}, {"some_id": 456, "someprop": "bar"}, {"some_id": 789, "someprop": "baz"}, ] and given a…
shacker
  • 14,712
  • 8
  • 89
  • 89
2
votes
1 answer

Optimising API queries using JSONField()

Initial opening: I am utilising postgresql JSONFields. I have the following attribute (field) in my User model: class User(AbstractUser): ... benefits = JSONField(default=dict()) ... I essentially currently serialize benefits for each User…
2
votes
1 answer

Is there a django-orm lookup for querying nested jsonfield key?

My table has a JsonField column named meta_data. One of its entries is: {'abc':'zyz', 'name':{'pranav':'age', 'john':'age'}} To query on Jsonfield i use __has_key lookup: table.objects.filter(id__in=id_list, meta_data__has_key='name') I want to…
2
votes
1 answer

Django JSON field in Serializer validated_data is None

I have a Django Model which looks something like this: class Foo: data = JSONField(encoder=DjangoJSONEncoder, default=dict) And I have the corresponding serializer for it which is pretty basic: class FooSerializer(BaseSerializer): class Meta: …
2
votes
2 answers

Bulk update with JSONField in Django

whens = [When(pk=x, then=_json['info'][str(x)]) for x in ids] Clinics.objects.filter(pk__in=ids).update( info_json=Case(*whens, output_field=JSONField()) ) I want to perform bulk update using Case-When statements for JSONField(). When I do this…
Averin Maxim
  • 373
  • 4
  • 19
2
votes
3 answers

Django Postgresql JsonField query related dictionary keys

A part of the model that I have, which uses Django Model field, is like the following: class SalesModel(models.Model): some_data = models.PositiveIntegerField(db_index=True) some_other_data = models.CharField(max_length=50) json_data =…
2
votes
2 answers

Fetch Json Field value using Values in Django

I have a JSON field in my model and by using values option, I would like to get the key value present in the JSON field. Assume my JSON field value is: {"key1":"value1","key2":"value2"} MyClass.objects.values("field1","field2","JSON Key") Let…
Mallik Sai
  • 186
  • 1
  • 4
  • 16
2
votes
2 answers

Django: Dynamically set model instance attributes based on JSONField

I have a question on how to set model attributes dynamically when model instances are initiated. I am using a simple model with a native PostgreSQL JSONField: from django.db import models from django.contrib.postgres.fields import JSONField class…
nikolas-berlin
  • 1,080
  • 8
  • 6
2
votes
0 answers

How/best way to migrate away from an old requirement (django-json-field) with django migration?

I am currently on Django 1.7 and my database is PostgreSQL. I have the following model. # myapp/models.py from django.db import models from json_field import JSONField # using django-json-field from json_field.fields import JSONDecoder class…
2
votes
1 answer

django postgres JSONField | query to check in list(contains) of values

In django 1.9(using postgresdb), we're using JSONField(in a model) whose entries looks something like this: **Entry 1**(in a row of that table): data: { "key1": "value1", "key2": "value2", "key3": "value3", "key4": "value4" } **Entry 2**(in…
Neo
  • 5,070
  • 10
  • 46
  • 65
1
vote
1 answer

Get mean value for price in dict (Django JSONField)

I need to extract some products from orders with the same SKU number. orders=Order.objects.filter(products__contains=[{"sku":"002-2-1"}]) for e in orders: print(e.products) >>> [{'sku': 002-2-1, 'price': '366.00'}, {'sku': 002-2-1, 'price':…
Irina_Xena
  • 245
  • 1
  • 11
1
vote
1 answer

Aggregate sum of values from Django JSONField

Consider I have a simple model setup with JSONField from django.db import models import random def simple_json_callable(): return {"amount": random.randint(1, 100)} def nested_json_callable(): data = { "l1": { "l2":…
JPG
  • 82,442
  • 19
  • 127
  • 206