user.has_perms(perms)上的无关Django DB查询

时间:2014-10-17 06:08:22

标签: django django-orm django-authentication

查看调用user.has_perms(perms)时运行的SQL查询,我看到:

SELECT "auth_permission"."id", 
       "auth_permission"."name", 
       "auth_permission"."content_type_id", 
       "auth_permission"."codename", 
       "django_content_type"."id", 
       "django_content_type"."name", 
       "django_content_type"."app_label", 
       "django_content_type"."model" 
FROM   "auth_permission" 
       inner join "auth_user_user_permissions" 
               ON ( "auth_permission"."id" = 
                    "auth_user_user_permissions"."permission_id" ) 
       inner join "django_content_type" 
               ON ( "auth_permission"."content_type_id" = 
                    "django_content_type"."id" ) 
WHERE  "auth_user_user_permissions"."user_id" = %s 
ORDER  BY "django_content_type"."app_label" ASC, 
          "django_content_type"."model" ASC, 
          "auth_permission"."codename" ASC 

SELECT "django_content_type"."app_label", 
       "auth_permission"."codename" 
FROM   "auth_permission" 
       inner join "auth_group_permissions" 
               ON ( "auth_permission"."id" = 
                    "auth_group_permissions"."permission_id" ) 
       inner join "auth_group" 
               ON ( "auth_group_permissions"."group_id" = "auth_group"."id" ) 
       inner join "auth_user_groups" 
               ON ( "auth_group"."id" = "auth_user_groups"."group_id" ) 
       left outer join "django_content_type" 
                    ON ( "auth_permission"."content_type_id" = 
                         "django_content_type"."id" ) 
WHERE  "auth_user_groups"."user_id" = %s 

我的问题是:

  1. 这些查询到底在做什么?
  2. 为什么他们继续前进 每一个请求?有没有办法缓存这些结果?

0 个答案:

没有答案