Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用hasattr()检查用户类型_Python_Django - Fatal编程技术网

Python 使用hasattr()检查用户类型

Python 使用hasattr()检查用户类型,python,django,Python,Django,我的一些用户是学生。当用户创建学生配置文件时,将实例化StudentProfile类: class StudentProfile(models.Model): user = models.OneToOneField(settings.AUTH_USER_MODEL, primary_key=True) … 如何检查用户是否为学生 hasattr(request.user, 'StudentProfile') 即使登录的用户具有关联的StudentProfile,也返回Fals

我的一些用户是学生。当用户创建学生配置文件时,将实例化StudentProfile类:

class StudentProfile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL, primary_key=True)
    …
如何检查用户是否为学生

hasattr(request.user, 'StudentProfile')

即使登录的用户具有关联的StudentProfile,也返回False。

您就快到了-您只需要使用小写的
StudentProfile
而不是
StudentProfile

hasattr(request.user, 'studentprofile')
发件人:

如果未为
OneToOneField
指定
相关的\u name
参数,Django将使用当前模型的小写名称作为默认值