I have JSONField
on my Product
model and in this model, I have nested fields.
For example:
"data": {
'products':{
"name": "ProductA",
"color": "Brown",
"price": {
"end_user": 54,
"credit_cart": 60
},
"name": "ProductB",
"color": "Red",
"price": {
"end_user": 100,
"credit_cart":120
},
}
}
I want to order nested end_user
field.
this is my attempt:
Products.objects.order_by(RawSQL("data->>%s", ("data__price__end_user",)))
I found the answer:
queryset = queryset.annotate(
end_user=RawSQL("data->'products'->'price'->'->'end_user'", [])
).order_by('end_user')