django-jsonfield

django - Unsupported lookup for JSONField or join on the field not permitted

爱⌒轻易说出口 提交于 2021-02-09 11:10:16
问题 I am having a Json field in my models as - class Product(models.Model): ... detailed_stock = JSONField(load_kwargs={'object_pairs_hook': collections.OrderedDict},default=dict) I am having values in my database like - { "total":0, "5[1]":0 } I am trying to filter objects with total = 0, for that I tried - Product.objects.filter(detailed_stock__total = 0) but it throws error - Unsupported lookup 'total' for JSONField or join on the field not permitted. as per the documentation the following

Django Admin: JSONField default empty dict wont save in admin

时光怂恿深爱的人放手 提交于 2020-06-10 09:19:30
问题 in my model defn i have from django.contrib.postgres.fields import JSONField ..... ....... ......... media_data=JSONField(default=dict) I created a default admin When i attempt to save without touching the field, i get a this field is required error. It looks like a form validation issue because I can programatically save the model instance from code without issue. Why is this happening? have i missed something silly? 回答1: What happening. when dive into the source code. we can see the

django object is not JSON serializable error after upgrading django to 1.6.5

Deadly 提交于 2020-01-09 09:50:51
问题 I have a django app which was running on 1.4.2 version and working completely fine, but recently i updated it to django 1.6.5 and facing some wierd errors like below Actually i am getting this during user/client registration process in my site functionality Request URL: http://example.com/client/registration/ Django Version: 1.6.5 Exception Type: TypeError Exception Value: <Client: test one> is not JSON serializable Exception Location: /usr/lib/python2.7/json/encoder.py in default, line 184

Django db error: could not identify an equality operator for type json when trying to annotate a model with jsonfield

非 Y 不嫁゛ 提交于 2020-01-01 09:26:08
问题 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 field_to_count_by is not JSONField, normal int field. Any ideas how i can solve the issue and still use annotate? What annotate does behind the hood? 回答1: I ran into the same problem and finally (today) implemented a fake operator by running this

django - aggregate json field specific keys and order by the aggregation

一笑奈何 提交于 2019-12-23 03:40:11
问题 I have a model with the a field data of type JSONField from django.contrib.postgres.fields . The json structure is like so: {'aa': 1, 'bb': 2, 'cc': 4} I want to aggregate the sums of the aa and cc keys - so in this case, it will be 5. Also - i cannot promise that either aa or cc will be in the json. Is this possible? If so - i want to order by the aggregated data. Example: id: 1, data = {'aa': 1, 'bb': 2, 'cc':4} id: 2, data = {'aa': 3, 'bb': 2} id: 3, data = {'cc': 7} id: 4, data = {'bb': 7

SELECT on JSONField with Django

人盡茶涼 提交于 2019-12-23 02:51:12
问题 My application is heavily reliant on APIs that unpredictably make changes to the way they return data. For this reason, I've chosen to use PSQL and JSONFields with Django. I've seen plenty of examples/docs on how to filter by values in a JSONField, but I haven't seen any that allow me to SELECT on these values. What I know works; queryset.filter(jsonfield__key_name = 'value') What I want to know how to do; queryset.values('jsonfield__key_name') Thanks in advance! 回答1: The answer is a RawSQL

Ordering Django querysets using a JSONField's properties

自作多情 提交于 2019-12-13 20:19:29
问题 I have a model that kinda looks like this: class Person(models.Model): data = JSONField() The data field has 2 properties, name , and age . Now, lets say I want to get a paginated queryset (each page containing 20 people), with a filter where age is greater than 25, and the queryset is to be ordered in descending order. In a usual setup, that is, a normalized database, I can write this query like so: person_list_page_1 = Person.objects.filter(age > 25).order_by('-age')[:20] Now, what is the

Django db error: could not identify an equality operator for type json when trying to annotate a model with jsonfield

拜拜、爱过 提交于 2019-12-04 04:02:25
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 field_to_count_by is not JSONField, normal int field. Any ideas how i can solve the issue and still use annotate? What annotate does behind the hood? I ran into the same problem and finally (today) implemented a fake operator by running this as admin in the psql console : -- This creates a function named hashjson that transforms the -- json to texts

Django annotate count in JSONField with Postgres

浪子不回头ぞ 提交于 2019-12-01 09:26:33
Using Django I have a field which is of type JSONField. I am wanting to get a distinct count on a nested key/value in the json. With a normal field you can just do soemthing like the following model.objects.values('field_name')\ .annotate(total=Count('field_name')).order_by('-total') This does not work for a JSONField. Example Model class Pet(models.Model): data = JSONField() Example of data { 'name':'sparky', 'animal':'dog', 'diet':{ 'breakfast':'biscuits', 'dinner':'meat', } } Trying Pet.objects.values('data__diet__dinner')\ .annotate(total=Count('data__diet__dinner')).order_by('-total')

Django annotate count in JSONField with Postgres

五迷三道 提交于 2019-12-01 04:21:26
问题 Using Django I have a field which is of type JSONField. I am wanting to get a distinct count on a nested key/value in the json. With a normal field you can just do soemthing like the following model.objects.values('field_name')\ .annotate(total=Count('field_name')).order_by('-total') This does not work for a JSONField. Example Model class Pet(models.Model): data = JSONField() Example of data { 'name':'sparky', 'animal':'dog', 'diet':{ 'breakfast':'biscuits', 'dinner':'meat', } } Trying Pet