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
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 modern Django (>=1.9) @hynecker's answer is better.
For Django >= 1.8 you can use an annotation and the Length function:
from django.db.models.functions import Length
qs = MyModel.objects.annotate(text_len=Length('text_field_name')).filter(
text_len__gt=10)
Under the hood, Django uses is @Pratyush's suggested CHAR_LENGTH()
MariaDB (MySQL) function. But the Django Length
function will work for any Django-compatible db by using LENGTH()
for Postgres and other databases.
text_len__gt=10
in first place (order_by
). Any hint?
text_len
annotation in the same way you would use any other database field, so it works in order_by
or Sum
or whatever. To sort the results in decreasing text length order and return the length values: MyModel.objects.annotate(text_len=Length('text_field_name')).order_by('-text_len').values_list('text_len', flat=True)
.
Length
Transform.
Sum
. It's also extremely important for many cases. I had a case where I needed to pre-check the maximum data size that a query might return won't run the server out of memory, and a variant of this worked perfectly.
Length
operator. So it's called a transformation or a mapping. Aggregations reduce the number of records. Mappings do not.
Another way is:
MyModel.objects.extra(where=["CHAR_LENGTH(text) > 300"])
This can be used where the text lenght is more than 255 characters too.
A nice solution for Django >= 1.9 is possible by registering the builtin function Length
as a Transform for CharField
lookup.
Register the transformation in the project once. (The best place is probably models.py.)
from django.db.models import CharField
from django.db.models.functions import Length
CharField.register_lookup(Length, 'length')
Use:
result = MyModel.objects.filter(text__length__gt=10)
See exactly the same example in docs for Length as a transform.
It works correctly for all backends, compiled by LENGTH()
for most backends and by CHAR_LENGTH()
for MySQL. It is then registered for all subclasses of CharField automatically, e.g. for EmailField. The TextField
must be registered individually. It is safe to register the name "length", because a transform name could never shade or be shaded by an equally named field name or related field name.
The only disadvantage could be readability puzzle: Where did the "length" come from? (The lookup is global, but the same can be luckily safely registered repeatedly in more modules, if useful for readability, without any possible overhead at query runtime.)
Other similarly valuable solution is the hobs's above that is shorter if a registration counts and if a similar query is not used repeatedly.
You can use the regex filter to search for text of a particular length:
MyModel.objects.filter(text__regex = r'.{10}.*')
Caveat: for MySQL, the maximum length value is 255. Otherwise an exception is thrown:
DatabaseError: (1139, "Got error 'invalid repetition count(s)' from regexp")
Using raw strings (e.g., r'foo' instead of 'foo') for passing in the regular expression syntax is recommended.
Sep 7, 2012 at 9:25
I would solve the problem on your app server and not tax your database. You can do this by:
models_less_than_ten = []
mymodel = MyModel.objects.all()
for m in mymodel:
if len(m.text) > 10:
models_less_than_ten.append(m)
It would be much better and faster if you just add a column that pre-calculates(memoizes) the length of the text.
e.g.
class MyModel(models.Model):
text = models.TextField()
text_len = models.PositiveIntegerField()
def save(self, *args, **kwargs):
self.text_len = len(self.text)
return super(MyModel, self).save(*args, **kwargs)
MyModel.objects.filter(text_len__gt = 10) # Here text_len is pre-calculated by us on `save`
text
. This is updated everytime the text is modified. So when we query the model, we can filter by the text length, which WE have pre-calculated on our save
method.
Oct 10, 2012 at 11:49