2017-12-28 12 views
0

그래서 사용자 정의 사용자 모델을 구현하고 있으므로 튜토리얼 'this 자습서를 따라했습니다. Django 추상 사용자 모델이 마이그레이션되지 않습니다.

은 모델입니다 :

class UserManager(BaseUserManager): 
    """Define a model manager for User model with no username field.""" 

    use_in_migrations = True 

    def _create_user(self, email, password, **extra_fields): 
     """Create and save a User with the given email and password.""" 
     if not email: 
      raise ValueError('The given email must be set') 
     email = self.normalize_email(email) 
     user = self.model(email=email, **extra_fields) 
     user.set_password(password) 
     user.save(using=self._db) 
     return user 

    def create_user(self, email, password=None, **extra_fields): 
     """Create and save a regular User with the given email and password.""" 
     extra_fields.setdefault('is_staff', False) 
     extra_fields.setdefault('is_superuser', False) 
     return self._create_user(email, password, **extra_fields) 

    def create_superuser(self, email, password, **extra_fields): 
     """Create and save a SuperUser with the given email and password.""" 
     extra_fields.setdefault('is_staff', True) 
     extra_fields.setdefault('is_superuser', True) 

     if extra_fields.get('is_staff') is not True: 
      raise ValueError('Superuser must have is_staff=True.') 
     if extra_fields.get('is_superuser') is not True: 
      raise ValueError('Superuser must have is_superuser=True.') 

     return self._create_user(email, password, **extra_fields) 


class User(AbstractUser): 
    """User model.""" 

    username = None 
    email = EmailField(_('email address'), unique=True) 

    USERNAME_FIELD = 'email' 
    REQUIRED_FIELDS = [] 

    objects = UserManager() 

지금 python3 manage.py migrate를 실행하려고하고이 오류를 얻을 : 내 settings.py에서

[wtreston] ~/gdrive/mysite2/ $ python3 manage.py migrate 
Operations to perform: 
    Apply all migrations: admin, auth, contenttypes, reviews, sessions, users 
Traceback (most recent call last): 
    File "manage.py", line 22, in <module> 
    execute_from_command_line(sys.argv) 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line 
    utility.execute() 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv 
    self.execute(*args, **cmd_options) 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute 
    output = self.handle(*args, **options) 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 164, in handle 
    pre_migrate_apps = pre_migrate_state.apps 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__ 
    res = instance.__dict__[self.name] = self.func(instance) 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/migrations/state.py", line 218, in apps 
    return StateApps(self.real_apps, self.models) 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/migrations/state.py", line 295, in __init__ 
    raise ValueError("\n".join(error.msg for error in errors)) 
ValueError: The field admin.LogEntry.user was declared with a lazy reference to 'users.user', but app 'users' doesn't provide model 'user'. 
The field reviews.Answer.student was declared with a lazy reference to 'users.user', but app 'users' doesn't provide model 'user'. 
The field reviews.ReviewBlock.teacher was declared with a lazy reference to 'users.user', but app 'users' doesn't provide model 'user'. 
The field users.Student.user was declared with a lazy reference to 'users.user', but app 'users' doesn't provide model 'user'. 
The field users.Teacher.user was declared with a lazy reference to 'users.user', but app 'users' doesn't provide model 'user'. 

내가이 줄 AUTH_USER_MODEL = 'users.User' 있습니다. ForeignKeys를 사용하여 AUTH_USER_MODEL을 참조 할 때 먼저 from mysite import settings을 가져 와서 ForeignKey(settings.AUTH_USER_MODEL)을 입력하십시오.

도움이 될 것입니다. 감사합니다

+0

'users.User'에는'abstract = True'가 있습니까? – schwobaseggl

+0

@schwobaseggl이 내가 사용하고있는 두 가지 모델을 추가했는데, 그게 무슨 뜻인지 모르겠습니까? – wtreston

+0

마이그레이션 파일은 어떻게 생겼습니까? – Jason

답변

0

당신은 당신의 사용자 정의 사용자 모델 AbstractUser을 상속하고 UserManager를 상속하여 UserManager 은 아래와 같은 관리자를 수정하기위한 BaseUserManager을 상속됩니다

class MyUserManager(UserManager): 
    def create_user(self, email, password=None, **kwargs): 
     # override this method 

    def create_superuser(self, email, password, **kwargs): 
     # override this method 

이 함께 마이그레이션을 적용 먼저 마이그레이션 및 데이터베이스를 정리하고 변경.

+0

'마이그레이션 및 데이터베이스를 먼저 정리 한 다음이 변경 사항을 적용한 마이그레이션을 적용한다는 것은 무엇을 의미합니까? '가장 최근의 마이그레이션을 삭제할 수 있습니다. 그러나 데이터베이스 정리로 어떤 의미인지는 알 수 없습니다. – wtreston

+0

마이그레이션을 삭제해도 삭제되지 않습니다 데이터베이스에 존재하는 생성 된 테이블. 간단하게 데이터베이스의 모든 테이블을 드롭 할 수 있습니다 .. 신선한 시작처럼 – Gahan

+0

좋아, 내가 그랬어 – wtreston