Django可以在模型中延迟加载字段吗?

编程入门 行业动态 更新时间:2024-10-12 01:25:33
本文介绍了Django可以在模型中延迟加载字段吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的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可以在模型中延迟加载字段吗?

本文发布于:2023-10-18 15:12:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504598.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   模型   加载   Django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!