Use Length
from django.db.models.functions import Length
qs = queryset.annotate(text_len=Length('text_field')).filter(text_len__gt=10)
Only can use Django >= 1.8
Use extra
qs = queryset.extra(where=["CHAR_LENGTH(text_field) > 10"])
Use regex
inside filter
qs = queryset.filter(text_field__regex=r'.{10}.*')
Register Length
to CharField
lookup
from django.db.models import CharField
from django.db.models.functions import Length
CharField.register_lookup(Length, 'length')
qs = queryset.filter(text_field__length__gt=10)
Only can use Django >= 1.9
'backend > django' 카테고리의 다른 글
Django: 기존에 있는 값에 unique 필드 추가하기 (0) | 2019.02.04 |
---|---|
Django: show invalid message in django loginform (0) | 2018.07.23 |
Django: add attrs to django form (0) | 2018.07.22 |
Django: channels 2.0 with nginx + daphne (0) | 2018.06.20 |
Django: use ckeditor in django - django ckeditor (0) | 2018.05.24 |