我创建了一个自定义User
模型
class User(AbstractUser):
phone_no = models.CharField(blank=True, null=True, max_length=20, default="")
class Meta:
db_table = 'auth_user'
当我访问User
的对象时,auth_group
和auth_permission
会自动被选中。
from django import db
from example.models import User
import simplejson as json
In : print json.dumps(db.connection.queries)
Out: []
In : user = User.objects.get(id=1)
In : print json.dumps(db.connection.queries)
Out:
[
{
"time": "0.001",
"sql": "SELECT `auth_user`.`id`, `auth_user`.`password`, `auth_user`.`last_login`, `auth_user`.`is_superuser`, `auth_user`.`username`, `auth_user`.`first_name`, `auth_user`.`last_name`, `auth_user`.`email`, `auth_user`.`is_staff`, `auth_user`.`is_active`, `auth_user`.`date_joined`, `auth_user`.`phone_no` FROM `auth_user` WHERE `auth_user`.`id` = 1 "
},
{
"time": "0.000",
"sql": "SELECT `auth_group`.`id` FROM `auth_group` INNER JOIN `auth_user_groups` ON ( `auth_group`.`id` = `auth_user_groups`.`group_id` ) WHERE `auth_user_groups`.`user_id` = 1 "
},
{
"time": "0.001",
"sql": "SELECT `auth_permission`.`id` 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` = 1ORDER BY `django_content_type`.`app_label` ASC, `django_content_type`.`model` ASC, `auth_permission`.`codename` ASC"
}
]
如何停止此行为,因为当我使用select_related
User
是外键并迭代user.first_name
的结果时,它会变得很昂贵。顺便说一句,我不使用auth_group
和auth_permission
所以我不介意放弃它