【发布时间】:2014-04-24 23:43:02
【问题描述】:
我有一个为另一种方法设置数据的函数。它这样做是为了限制对数据库的调用。
设置方法如下:
def get_customers(request):
customer_list = Customer.objects.filter(pk=request.user)
populated_customer = get_customer(request, customer_list)
进行处理的方法如下所示:
def get_customer(request):
for customer in customer_list:
if customer.id == 3:
# do something with this customer
我要处理数百万条记录,而不是执行 for 循环来查找我需要的客户,而是如何在不进入数据库的情况下将其从列表中拉出。
【问题讨论】:
-
如果根据
get_customer视图,您只需要一个客户,为什么需要保留customer_list- 为什么不只通过数据库中的主键过滤客户?此外,您可以开始在 memcached 或 redis 等 key:value 存储中缓存客户。
标签: python django django-queryset django-orm