Django 用户个人资料
当向用户配置文件添加其他字段(例如位置、性别、雇主等)时,我是否应该向 django.contrib.auth.models.User 添加其他列并将其保存在那里?或者我应该创建一个新表来保存用户配置文件信息?
另外,当用户上传个人资料图片时,我应该将其保存在同一个表中吗? (请注意,这不是生产服务器,我只是在本地运行服务器上执行此操作以解决问题)。谢谢
When adding additional fields to a user profile, such as location, gender, employer, etc., should I be adding additional columns to django.contrib.auth.models.User
and saving it there? Or should I be creating a new table to save user profile information?
Also, when a user uploads a profile picture, should I be saving this in the same table? (Note this is not a production server, I'm just doing this on my local runserver to figure things out). Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
你必须为用户配置文件建立一个模型:
然后在
settings.py
中配置:You have to make a model for the user profile:
Then configure in
settings.py
:当前 Django 是 1.9,这里是对过时的接受答案的一些更新
models.OneToOneField(User)
lated_name='profile'
.__str__()< Python 3 的 /code> 和
.format()
如下所示
使用
lated_name
您可以轻松访问用户的个人资料,例如request.user
不需要额外的查找。
Current Django is 1.9 and here are some updates to the outdated accepted answer
models.OneToOneField(User)
related_name='profile'
.__str__()
and.format()
for Python 3like so
Using
related_name
you can access a user's profile easily, for example forrequest.user
No need for additional lookups.
Django 提供了一种将有关用户的附加信息存储在单独的文件中的方法表(称为用户配置文件)。
Django provides a way of storing additional information about users in a separate table (called user profile).
从 Django 1.5 开始,您可以使用简单的设置条目将默认 User 替换为自定义用户对象:
有关更多详细信息,请检查此 Django 文档条目。
Starting with Django 1.5 you can replace the default User with your custom user object using a simple settings entry:
For slightly more details, check this Django documentation entry.
我在此处找到了一个解决方案。基本上,您只需扩展默认表单 UserCreationForm 但保持相同的名称。它与 Django 文档告诉您执行 UserProfiles 的方式无缝配合。
There's a solution I found here. Basically you just extend the default form
UserCreationForm
but keeping the same name. It works seamlessly with the way Django's docs tell you to doUserProfiles
.可以更新答案以添加信号接收器,如果配置文件不存在,则该接收器将创建配置文件;如果配置文件已存在,则更新配置文件。
这个 https:// /simpleisbetterthancomplex.com/tutorial/2016/11/23/how-to-add-user-profile-to-django-admin.html 帖子还包括如何编辑、在管理面板中列出自定义配置文件。
Answer can be updated to add signal receiver which will create the profile if it does not exist and update if it is already there.
This https://simpleisbetterthancomplex.com/tutorial/2016/11/23/how-to-add-user-profile-to-django-admin.html post also includes how to edit, list the custom profile in admin panel.
当前 2 个热门答案已过时
https://docs.djangoproject.com /en/3.2/topics/auth/customizing/#referencing-the-user-model
The current 2 top answers are outdated
https://docs.djangoproject.com/en/3.2/topics/auth/customizing/#referencing-the-user-model
如果您想从用户对象中获取用户配置文件数据。
If you want to get user profile data from user objects.