[英]get user profile in django
hello i'm new in python and django I need a view that get current user profile I know I shoud use get_profile from User but I don't know how to use it . 你好我是python和django的新手我需要一个获取当前用户配置文件的视图我知道我应该使用来自User的get_profile但我不知道如何使用它。 i read the django document and It didn't help me.
我读了django文件,它没有帮助我。 this is what I found from doc:
这是我从doc中找到的:
from django.contrib.auth.models import User
profile=request.user.get_profile()
Django's documentation says it all, specifically the part Storing additional information about users . Django的文档说明了这一切,特别是存储有关用户的其他信息的部分。 First you need to define a model somewhere in your
models.py
with fields for the additional information of the user: 首先,您需要在
models.py
某个位置定义模型,其中包含用户附加信息的字段:
models.py models.py
from django.contrib.auth.models import User
class UserProfile(models.Model):
# This field is required.
user = models.OneToOneField(User)
# Other fields here
accepted_eula = models.BooleanField()
favorite_animal = models.CharField(max_length=20, default="Dragons.")
Then, you need to indicate that this model ( UserProfile
) is the user profile by setting AUTH_PROFILE_MODULE
inside your settings.py
: 然后,您需要通过在
settings.py
设置AUTH_PROFILE_MODULE
来指示此模型( UserProfile
)是用户配置文件:
settings.py settings.py
...
AUTH_PROFILE_MODULE = 'accounts.UserProfile'
...
You need to replace accounts
with the name of your app. 您需要使用应用名称替换
accounts
。 Finally, you want to create a profile every time a User
instance is created by registering a post_save
handler, this way every time you create a user Django will create his profile too: 最后,您希望在每次创建
User
实例时通过注册post_save
处理程序来创建配置文件,这样每次创建用户时Django也会创建他的配置文件:
models.py models.py
from django.contrib.auth.models import User
class UserProfile(models.Model):
# This field is required.
user = models.OneToOneField(User)
# Other fields here
accepted_eula = models.BooleanField()
favorite_animal = models.CharField(max_length=20, default="Dragons.")
def create_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
post_save.connect(create_user_profile, sender=User)
To access the current user's profile in your view, just use the User
instance provided by the request, and call get_profile on it: 要在视图中访问当前用户的配置文件,只需使用请求提供的
User
实例,并在其上调用get_profile :
def your_view(request):
profile = request.user.get_profile()
...
# Your code
Basically django User models will provide access only for the fields ( firstname,lastname,email,password,is_staff,is_active,last_login). 基本上django用户模型将仅为字段提供访问权限(firstname,lastname,email,password,is_staff,is_active,last_login)。
However if we want to add any extra fields to this model, say we need to add a new column named dateofbirth for every user, then we need to add a column named DOB into User model. 但是,如果我们要向此模型添加任何额外字段,请说我们需要为每个用户添加一个名为dateofbirth的新列,然后我们需要在User模型中添加一个名为DOB的列。 But this is not possible as we aren't able to edit django User models.
但这是不可能的,因为我们无法编辑django用户模型。
To achieve this either 要实现这一点
1.We can have a separate new table with email id & DOB column, such that a column in User model is mapped with a column in the new table. 1.我们可以有一个单独的新表,其中包含电子邮件ID和DOB列,以便用户模型中的列与新表中的列进行映射。 But this will create a new db instance for every db request.
但是这将为每个数据库请求创建一个新的数据库实例。 Say if u want to find the DOB of a customer,
如果你想找到客户的DOB,
In the second method, 在第二种方法中,
Instead of using django User model, use your own customize model with all the fields needed. 不使用django用户模型,而是使用您自己的自定义模型以及所需的所有字段。 However if any updation related to security or some enhancement made to django User model we can't use it directly.
但是,如果任何与安全性相关的更新或对django User模型进行的某些增强,我们就无法直接使用它。 We need to do more code changes at our end( wherever we use our customize models.) This will be a bit pain for a developer to identify the code & make changes.
我们需要在最后(无论我们使用自定义模型的地方)进行更多代码更改。对于开发人员来说,识别代码并进行更改会有点麻烦。
To overcome the above issues, django introduce django profile which is very simple and more flexible. 为了克服上述问题,django引入了django profile,它非常简单,更灵活。 The advantages are
优点是
How to Use this: 如何使用:
In settings.py create a variable AUTH_PROFILE_MODULE = "appname.profiletable" 在settings.py中创建一个变量AUTH_PROFILE_MODULE = “appname.profiletable”
Say, we created a new table extrauser which has DOB, emailid. 说,我们创建了一个新的表extrauser,它有DOB,emailid。 To find the DOB of a customer, use
要查找客户的DOB,请使用
a=User.objects.get(email='x@x.xom')
a.get_profile().DOB will give the dateofbirth value from extrauser table.
Hope the above details make you clear in understanding django profile. 希望以上细节让您清楚了解django个人资料。 Incase of any help further, let me know.
如果有任何帮助,请告诉我。 I have used django profile in my project.
我在我的项目中使用过django profile。
Old question but I thought anyone seeing it today may benefit from this: 老问题,但我认为今天有人看到它可能会从中受益:
Django 1.5 adds the ability to - easily - extend the User model. Django 1.5增加了 - 轻松 - 扩展用户模型的能力。 This may be preferable as you now only got one object to deal with rather than two!
这可能更好,因为你现在只有一个对象而不是两个! Seems the more modern way.
似乎更现代的方式。
https://hurricanelabs.com/blog/django-user-models/ https://hurricanelabs.com/blog/django-user-models/
You need to specify which class is your "Profile" by setting AUTH_PROFILE_MODULE = 'accounts.UserProfile'
(for example) 您需要通过设置
AUTH_PROFILE_MODULE = 'accounts.UserProfile'
(例如)来指定哪个类是您的“配置文件”
https://docs.djangoproject.com/en/1.4/topics/auth/ https://docs.djangoproject.com/en/1.4/topics/auth/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.