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 is a Char or Text field in MyModel model
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:
CHAR_LENGTH()
LENGTH()
from django.db.models.functions import Length qs = MyModel.objects.annotate(text_len=Length('text_field_name')).filter( text_len__gt=10)
2.1m questions
2.1m answers
60 comments
57.0k users