28👍
for Django >= 1.11, you can use:
from django.contrib.postgres.fields.jsonb import KeyTextTransform
MyModel.objects.annotate(val=KeyTextTransform('key1', 'jsonfield')).order_by('val')
3👍
An updated answer for this question:
At current time of writing (Django 3.1), you can now order exactly as you had hoped without needing any helper functions:
MyModel.objects.order_by('jsonfield__key1')
- How to profile django channels?
- How to find out whether a model's column is a foreign key?
- Changing password in Django Admin
- Django rest auth email instead of username
- Celery workers unable to connect to redis on docker instances
0👍
JSONExtract is use with mysql
Syntax (‘json-field-name’, ‘json-field-key’)
~Q is used for "not equal"
from django_mysql.models.functions import JSONExtract
Listing.objects.annotate(weight=JSONExtract(‘package_dimensions’,’$.p>ackage_weight’)).filter(~Q(weight=0))
- Getting scrapy project settings when script is outside of root directory
- Apache strips down "Authorization" header
- How to choose the value and label from Django ModelChoiceField queryset
- ERROR: Invalid HTTP_HOST header: '/webapps/../gunicorn.sock'
- How to Test Stripe Webhooks with Mock Data
Source:stackexchange.com