我的django模型中有一个很大的 TextField ,我通常不需要使用它。有没有办法告诉Django延迟加载此字段?即除非我明确要求,否则不要麻烦从数据库中拉出它。每次我引用这些对象时,我都在浪费大量内存和带宽将 TextField 拖入python。
One of my django models has a large TextField which I often don't need to use. Is there a way to tell django to "lazy-load" this field? i.e. not to bother pulling it from the database unless I explicitly ask for it. I'm wasting a lot of memory and bandwidth pulling this TextField into python every time I refer to these objects.
另一种方法是为该字段的内容创建一个新表,但是如果可以的话,我宁愿避免这种复杂性。
The alternative would be to create a new table for the contents of this field, but I'd rather avoid that complexity if I can.
推荐答案使用 defer()语句而不是模型定义进行查询时,功能会发生。在文档中查看: http:// docs.djangoproject/en/dev/ref/models/querysets/#defer
The functionality happens when you make the query, using the defer() statement, instead of in the model definition. Check it out here in the docs: docs.djangoproject/en/dev/ref/models/querysets/#defer
现在,实际上,您是重构和提取数据的替代解决方案放入另一个表是一个非常好的解决方案。有人会说需要延迟加载字段意味着存在设计缺陷,并且应该对数据进行不同的建模。
Now, actually, your alternative solution of refactoring and pulling the data into another table is a really good solution. Some people would say that the need to lazy load fields means there is a design flaw, and the data should have been modeled differently.
无论哪种方法都可以!
Either way works, though!
更多推荐
Django可以在模型中延迟加载字段吗?
发布评论