0

i have a model and this model have a JSONField

class Book(models.Model):
    data = JSONField()

And this field have an array.

data = {
     'outlet':[
     {'price': 100},
     {'price': 200},
     {'price': 300}
     ]}

I want to get min value of outlet price.I've tried use KeyTextTransform and Min func. I can do easily if outlet not an array. But unfortunately i have to work on array in this case.

I want to solve this problem with ORM. If this is impossible, i'll solve this problem with pythonic As a last resort.

Utkucan Bıyıklı
  • 1,072
  • 7
  • 17
  • Possible duplicate of [In List of Dicts, find min() value of a common Dict field](https://stackoverflow.com/questions/5320871/in-list-of-dicts-find-min-value-of-a-common-dict-field) – michjnich Aug 20 '19 at 06:50

1 Answers1

-1

try this: you can first map the list of dicts to a list of prices (integers)

map(lambda outlet: outlet['price'], data['outlet'])

than you can simply use the min function to extract the minim price possible.

min(map(lambda outlet: outlet['price'], data['outlet']))

ruhaib
  • 604
  • 5
  • 12