django filter on the basis of text length

前端 未结 6 1098
囚心锁ツ
囚心锁ツ 2020-12-07 19:42

I would like to filter my model on the basis of the length of the text Something like

MyModel.objects.filter(len(text) > 10)

where text

6条回答
  •  青春惊慌失措
    2020-12-07 20:24

    For Django >= 1.8 you can use the Length function, which is @Pratyush's CHAR_LENGTH() under the hood for MySQL, or LENGTH() for some other databases:

    from django.db.models.functions import Length
    qs = MyModel.objects.annotate(text_len=Length('text_field_name')).filter(
        text_len__gt=10)
    

提交回复
热议问题