【发布时间】:2011-05-08 16:20:51
【问题描述】:
我在我的一个模型上动态覆盖了 Django 的 get_query_set 函数。我这样做是为了使用装饰器通过“场景”值强制过滤 Model.objects.all/filter/get 返回的原始查询集。这是装饰器的功能:
# Get the base QuerySet for these models before we modify their
# QuerySet managers. This prevents infinite recursion since the
# get_query_set function doesn't rely on itself to get this base QuerySet.
all_income_objects = Income.objects.all()
# Figure out what scenario the user is using.
current_scenario = Scenario.objects.get(user=request.user, selected=True)
# Modify the imported income class to filter based on the current scenario.
Expense.objects.get_query_set = lambda: all_expense_objects.filter(scenario=current_scenario)
# Call the method that was initially supposed to
# be executed before we were so rudely interrupted.
return view(request, **arguments)
我这样做是为了干燥代码,这样我的所有查询都不会被额外的过滤器弄得乱七八糟。但是,如果场景发生变化,则不会返回任何对象。如果我杀死服务器上的所有 python 进程,则会出现新选择场景的对象。我认为它正在缓存修改后的类,然后当场景发生变化时,它会应用另一个永远不会有意义的过滤器,因为对象一次只能有一个场景。
这不是基于用户的过滤器的问题,因为用户永远不会在我的会话中更改。乘客是否在做一些愚蠢的事情来在请求之间保留类对象?我是否应该放弃这种奇怪的设计模式,只在每个视图的基础上实现这些过滤器?必须有一个 DRYing 过滤器的最佳实践,该实践适用于基于动态事物(例如当前用户)的许多视图。
【问题讨论】:
-
我没看到装饰器在哪里?
标签: python django filter dry django-queryset