Django:强制选择相关?
我已经创建了一个模型,并且正在为其渲染默认/未修改的模型表单。仅此一项就生成了 64 个 SQL 查询,因为它有相当多的外键,而这些查询又具有更多的外键。
每次返回这些模型之一时,是否可以强制它始终(默认情况下)执行select_lated
?
I've created a model, and I'm rendering the default/unmodified model form for it. This alone generates 64 SQL queries because it has quite a few foreign keys, and those in turn have more foreign keys.
Is it possible to force it to always (by default) perform a select_related
every time one of these models are returned?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以创建一个自定义管理器,然后只需覆盖
get_queryset
即可将其应用到任何地方。例如:(在 Django 1.6 之前,它是
get_query_set
)。You can create a custom manager, and simply override
get_queryset
for it to apply everywhere. For example:(Prior to Django 1.6, it was
get_query_set
).这还有一个有趣的技巧:
然后您可以在模型类之间轻松地重复使用管理器。作为示例用例,如果您在模型上有一个
__unicode__
方法,该方法呈现一个包含来自相关模型的一些信息的字符串(或任何其他意味着相关模型几乎< em>总是需要)。...如果您真的想要变得古怪,这里有一个更通用的版本。它允许您使用
args
或kwargs
的任意组合在默认查询集上调用任何方法序列。代码中可能存在一些错误,但您明白了。受 python-mock 启发的
MethodCalls
对象是使 API 更加自然的尝试。有些人可能会觉得这有点令人困惑。如果是这样,您可以将该代码替换为仅接受方法调用信息元组的__init__
arg 或 kwarg。Here's also a fun trick:
Then you can re-use the manager easily between model classes. As an example use case, this would be appropriate if you had a
__unicode__
method on the model which rendered a string that included some information from a related model (or anything else that meant a related model was almost always required)....and if you really want to get wacky, here's a more generalized version. It allows you to call any sequence of methods on the default queryset with any combination of
args
orkwargs
. There might be some errors in the code, but you get the idea.The python-mock-inspired
MethodCalls
object is an attempt at making the API more natural. Some might find that a bit confusing. If so, you could sub out that code for an__init__
arg or kwarg that just accepts a tuple of method call information.创建自定义
models.Manager
并覆盖所有方法(filter
、get
等)并将 select_lated 附加到每个查询上。然后将此管理器设置为模型上的objects
属性。我建议只检查您的代码并在需要的地方添加
select_lated
,因为对所有内容执行 select_lated 都会导致一些严重的性能问题(并且不完全清楚它会发生在哪里)从)。Create a custom
models.Manager
and override all the methods (filter
,get
etc.) and append select_related onto every query. Then set this manager as theobjects
attribute on the model.I would recommend just going through your code and adding the
select_related
where needed, because doing select_related on everything is going to cause some serious performance issues down the line (and it wouldn't be entirely clear where it's coming from).