在旧版本的 Django 中,您可以扩展 User 模型并创建 UserProfile。现在看来,在Django 1.8中,这几乎不再相同了。我正在寻找在Django 1.8中执行此操作的好示例。
这是我在网上某处找到的一个示例,但它没有扩展任何内容。我只想扩展用户,以便我仍然可以在我的模板等中使用用户对象。
class MyUser(AbstractBaseUser):
"""
Custom user class.
"""
GENDER_CHOICES = (
('M', 'Male'),
('F', 'Female'),
)
email = models.EmailField('email address', unique=True, db_index=True)
is_staff = models.BooleanField('is staff', default=False)
first_name = models.TextField('first name', default=None, null=True)
last_name = models.TextField('last name', default=None, null=True)
date_of_birth = models.DateField('date of birth', null=True)
avatar = models.ImageField('profile picture', upload_to='static/media/images/avatars/', null=True, blank=True)
has_picture = models.BooleanField('has profile picture', default=False)
adult = models.BooleanField('is adult', default=False)
gender = models.CharField('gender', max_length=1, choices=GENDER_CHOICES)
objects = MyUserManager()
REQUIRED_FIELDS = ['date_of_birth', 'gender']
USERNAME_FIELD = 'email'
# Insert a lot of methods here
def set_avatar(self):
self.has_picture = True
使用以下 decoumentation:https ://docs.djangoproject.com/en/1.6/topics/auth/customizing/#extending-django-s-default-user
给了我所有这些错误:
Rendering model states...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/hermanstander/apps/afriapps/stem/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/Users/hermanstander/apps/afriapps/stem/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/hermanstander/apps/afriapps/stem/env/lib/python2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/hermanstander/apps/afriapps/stem/env/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "/Users/hermanstander/apps/afriapps/stem/env/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 221, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/Users/hermanstander/apps/afriapps/stem/env/lib/python2.7/site-packages/django/db/migrations/executor.py", line 100, in migrate
state.apps # Render all real_apps -- performance critical
File "/Users/hermanstander/apps/afriapps/stem/env/lib/python2.7/site-packages/django/utils/functional.py", line 60, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/hermanstander/apps/afriapps/stem/env/lib/python2.7/site-packages/django/db/migrations/state.py", line 166, in apps
return StateApps(self.real_apps, self.models)
File "/Users/hermanstander/apps/afriapps/stem/env/lib/python2.7/site-packages/django/db/migrations/state.py", line 248, in __init__
raise ValueError(msg.format(field=operations[0][1], model=lookup_model))
ValueError: Lookup failed for model referenced by field admin.LogEntry.user: app.UserProfile