基于文本长度的django过滤器 [英] django filter on the basis of text length
本文介绍了基于文本长度的django过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想根据文本的长度过滤我的模型
像
I would like to filter my model on the basis of the length of the text Something like
MyModel.objects.filter(len(text) > 10)
其中text是Char或Text字段在MyModel模型中
where text is a Char or Text field in MyModel model
提前感谢
推荐答案
如果您只是添加一个预先计算(记住)文本长度
的列,那么更好的和更快。
It would be much better and faster if you just add a column that pre-calculates(memoizes) the length of the text.
eg
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`
这篇关于基于文本长度的django过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文