python - 在 Django 中,如何查看用户的所有属性?

标签 python django django-models

如果我这样做:

print request.user.is_authenticated()

然后打印 True。然而

print request.user

即使'request.user'绝对不仅仅是一个字符串,也只是打印用户名。如何将所有用户属性打印到控制台?

最佳答案

方法有很多种,例如:

print dir(request.user)

或(这可能是首选)

print request.user.__dict__

或者(参见 stalk 的扩展答案,基本上做 __dict__ 做的)

print ["{0}: {1}".format(field.name, getattr(request.user, field.name)) for field in request.user._meta.fields]

或者(如果你只是想要方法)

print [attr for attr in dir(request.user) if callable(attr)]

关于python - 在 Django 中,如何查看用户的所有属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16520441/

相关文章:

python - Pandas 浮点小数

python - 如何组合一维数组来创建一个新数组

python - + : 'NoneType' and 'int' 不支持的操作数类型

python - Django case when else in filter

python - 如何使用 pytest 在 Django 中测试重定向?

javascript - 如何使用 Python 搜索文件中的 JavaScript 变量?

javascript - 如何在 Django 中使用由 html 和 css 制作的注册表单

Django 外键打破多表继承

django - 如何使用 django 建立对称关系模型?

python - 将通过 PIL 创建的图像保存到 django 模型