简体   繁体   English

多个vs一个大context_processor

[英]Multiple vs one big context_processor

I have multiple context processor and in each I have to request the user. 我有多个context processor ,在每个context processor中我都必须请求用户。 Each of these look like this: 这些每个看起来像这样:

def UploadForm(request):
  user = request.user
  Uplo = UploadForm(request.POST or None, initial={user})
  return {'Uplo': Uplo}

I saw that this is not efficient since im requesting the user multiple times, so I thought about writing one big context processor where I define all the Forms at once. 我看到这是无效的,因为我多次请求用户,所以我考虑编写一个大型context processor ,在该context processor中我一次定义所有窗体。

def AllForms(request):
  user = request.user
  Uplo = UploadForm(request.POST or None, initial={user...})
  SetForm = SetForm(request.POST or None, initial={user...})
  ...
  return {'Uplo': Uplo,'SetForm': SetForm}

Can anybody tell me if I gain here anything? 有人可以告诉我我在这里有什么收获吗? What is the common standard for context processors? 上下文处理器的通用标准是什么? I could not find anything on SO. 我在SO上找不到任何东西。

Getting user from request is not a big thing. 从请求中获取用户并不是一件大事。 It is o(1) operation. 它是o(1)操作。

However if the multiple context processors are not doing different thing and can be don at one time, it should be better to create one big context processor as you say it. 但是,如果多个上下文处理器没有做不同的事情并且可以一次执行,那么最好创建一个大型上下文处理器。 The reason being you have to get in and out of the function multiple times in same request. 原因是您必须在同一请求中多次进入和退出该功能。

Anyway if you want definitive difference, you can just print time in multiple and clubbed context processors. 无论如何,如果您想要确定性的差异,则可以只在多个上下文环境处理器中打印时间。

And yes, if you are hitting the database every time, you should club them and optimise the number of times you have to hit the db. 是的,如果您每次都访问数据库,则应将它们合并在一起并优化必须访问数据库的次数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM