我正在尝试遵循文档并在我的模型中设置 UserProfile 表,将其与管理区域中的 User 表相关联,然后在我的用户注册时将有关我的用户的其他信息存储在此 UserProfile 表中。
在views.py
我有以下内容:
from django.contrib.auth import authenticate, login, logout
def register(request):
if request.method == 'POST':
query_dict = request.POST
username = query_dict.__getitem__("username")
email = query_dict.__getitem__("user_email")
password = query_dict.__getitem__("password")
repeat_password = query_dict.__getitem__("repeat_password")
role = query_dict.__getitem__("role")
user = User.objects.create_user(username, email, password)
# django.db.models.signals.post_save gets called here and creates the UserProfile
# I can write something like user_profile = user.get_profile() but I don't
# know how to save information to the profile.
user = authenticate(username=username, password=password)
if user is not None and user.is_active:
login(request, user)
return HttpResponseRedirect("/")
正如您在上面代码中的注释中看到的那样,我可以检索关联的 UserProfile 对象,但我不知道从那里去哪里将附加数据(角色)存储在 UserProfile 表中。所有文档告诉我的是:
get_profile() 返回此用户的站点特定配置文件。如果当前站点不允许配置文件,则引发 django.contrib.auth.models.SiteProfileNotAvailable,如果用户没有配置文件,则引发 django.core.exceptions.ObjectDoesNotExist。
你可以在这里查看:https ://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.models.User.get_profile
但是文档没有告诉我get_profile()
返回什么样的对象,或者我如何使用它在 UserProfile 表中存储信息。