AttributeError:类型对象“ UserProfile”没有属性“ USERNAME_FIELD”

时间:2019-04-10 16:24:33

标签: python django django-models

我刚开始使用自定义用户模型。在运行make migrations和migration之后,出现以下错误: AttributeError:类型对象'UserProfile'没有属性'USERNAME_FIELD'

这是我的account / models.py

from django.contrib.auth.models import User


# Create your models here.
class UserProfile(models.Model):

    REQUIRED_FIELDS = ('user',)
    user = models.OneToOneField(User, on_delete=models.CASCADE)

    def __str__(self):
        return self.user.username

在TaskList / models.py中,我试图将用户用作外键:

from django.utils import timezone
from Account.models import UserProfile
from django.contrib.auth import get_user_model

class ToDoList(models.Model):
    title        = models.CharField(max_length=120)
    description  = models.TextField(help_text='Explain your task!', blank=True)
    created_date = models.DateTimeField(default=timezone.now())
    due_date     = models.DateTimeField(default=timezone.now())
    completed    = models.BooleanField(default=False)
    Author       = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)

在我的settings.py中,我添加了: AUTH_USER_MODEL = 'Account.UserProfile'

0 个答案:

没有答案