标签:查询 col value user logs fetch rom name object
user_list = models.UserInfo.object.all()
for row in user_list:
#只能取当前表数据,否则性能会非常低
select_related,主动连表查询【FK】
user_list = models.UserInfo.objects.all().select_related("FK字段")
for row in user_list:
#只能取当前表数据和FK表关联字段
user_list = models.UserInfo.objects.values(...)
for row in user_list:
#只去取当前表数据和FK表关联字段
===》 连表降低性能
prefetch_related:
user_list = models.UserInfo.objects.all().prefetch_related(‘FK字段‘)
#查询用户表models.UserInfo.objects.all() 1000个obj
#把用户表中所有的ut_id拿到,用户类型ID [1,2,3]
#select * from UserType where id in [1,2,3]
user_list = models.UserInfo.objects.all().prefetch_related(‘ut‘,‘xx‘)
for row in user_list:
print(row.name, row.pwd, row.ut.caption)
补充:
#[obj,obj,obj]
user_list = models.UserInfo.objects.all().only(‘name‘) # 只取某个字段
user_list = models.UserInfo.objects.all().defer(‘name‘) # 排除当前字段
for row in user_list:
print(row.pwd)
标签:查询 col value user logs fetch rom name object
原文地址:http://www.cnblogs.com/guomeina/p/7563370.html