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`
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.