Notice
Recent Posts
Recent Comments
Link
«   2025/11   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
Tags
more
Archives
Today
Total
관리 메뉴

Finn의 개발블로그

json depth 2이상 필드 오더링 하기 본문

Django

json depth 2이상 필드 오더링 하기

BeginnerFinn 2018. 10. 15. 16:06

django 2.1이 되면서 json field에 대해서 대부분을 지원하지만 order_by에 대해서는 지원하지 않아서 찾아봤습니다


django model


class Country(model.Model)

name = models.CharField(max_length=255)

data = JSONField(default=dict)


json example

{

"country":

{

"city_number":12

}

}


Solution 


Country.objects.all().annotate(city_number=KeyTransform('city_number', KeyTextTransform('country', 'data'))).distinct().order_by('city_number')