我正在尝试将其他字段添加到默认的用户模型中。我浏览了一下互联网并做了类似的事情:
在/UserProfile/models.py中创建了一个名为UserProfile的新模型,其中包含以下内容:
from django.db import models
from django.contrib.auth.models import User
class UserProfile(models.Model):
# This is the only required field
user = models.ForeignKey(User, unique=True)
# The rest is completely up to you...
favorite_band = models.CharField(max_length=100, blank=True)
favorite_cheese = models.CharField(max_length=100, blank=True)
lucky_number = models.IntegerField()
并补充道:
AUTH_PROFILE_MODULE = "newSite.userprofile"
对于设置,我还将“UserProfile”添加到INSTALLED_APPS以运行sqlall。
唯一的问题是当我在shell中尝试以下内容时:
>>> from django.contrib.auth.models import User
>>> user = User.objects.get(username = "user01")
>>> user.get_profile()
我收到以下错误:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Python27\lib\site-packages\django\contrib\auth\models.py", line 367,in get_profile
raise SiteProfileNotAvailable('Unable to load the profile '
SiteProfileNotAvailable: Unable to load the profile model, check AUTH_PROFILE_MODULE in your project settings
感谢您解决此问题的任何帮助!
答案 0 :(得分:4)
如果您确实创建了一个名为UserProfile的app并将models.py放在那里,那么您应该放
AUTH_PROFILE_MODULE = "userprofile.userprofile"
改为settings.py
文件。
您应该将UserProfile文件夹设为小写:userprofile