2013-03-08 6 views
0

내 사용자 정의 사용자 모델을 작성하여 django 1.5를 망치고 있습니다. 내 모델에는 필수 필드 인 dob = DateTimeField()가 있습니다. 나는 거의 UserManager와 AbstractUser를 동일하게 유지했다. 내가 manage.py createsuperuser를 파이썬 할 때 그러나, 내가하지 입력 DOB을하고 내가 오류 얻을 :수퍼 유저의 생년월일을 입력하도록 django UserManager를 구성 하시겠습니까?

IntegrityError : 열 "DOB '에서 null 값은 어떻게하지 null이 제약을

을 위반 I UserManager를 편집하여 터미널에서 생년월일을 입력 할 수있게하고 db에 DateTimeField로 저장할 입력을 구성합니까?

편집 내 대답 : 당신은 AbstractUser에서 상속 모델에 REQUIRED_FIELDS을 덮어 쓸 필요가

class UserManager(BaseUserManager): 

    def create_user(self, username, email=None, password=None, dob=None, **extra_fields): 
     """ 
     Creates and saves a User with the given username, email and password. 
     """ 
     now = timezone.now() 
     if not username: 
      raise ValueError('The given username must be set') 
     if not email: 
      raise ValueError('Email must be given') 
     email = UserManager.normalize_email(email) 
     user = self.model(username=username, email=email, 
          is_staff=False, is_active=True, is_superuser=False, 
          last_login=now, date_joined=now, dob=dob, **extra_fields) 

     user.set_password(password) 
     user.save(using=self._db) 
     return user 

    def create_superuser(self, username, email, password, dob, **extra_fields): 
     u = self.create_user(username, email, password, dob, **extra_fields) 
     u.is_staff = True 
     u.is_active = True 
     u.is_superuser = True 
     u.save(using=self._db) 
     return u 


# ... in the user model: 
... 
    REQUIRED_FIELDS = ['email', 'dob'] 
... 

답변

1

. contrib/auth/models.py을 통해 브라우징하면 이 REQUIRED_FIELDS = []이고 AbstractUserREQUIRED_FIELDS = ['email'] 인 방법을 볼 수 있습니다. 더 이상 아니 추측 :

class MyCoolUserModel(AbstractUser): 
    REQUIRED_FIELDS = ['email', 'dob'] 
    ... 

편집 : 내 생각 엔 당신이 뭔가를해야 할 것이다. 그것은 작동합니다.

일반적으로 createsuperuser의 내용은 contrib/auth/management/commands/createsuperuser.py입니다. 비인증 명령은 core/management/commands/에 있습니다. 거의 모든 manage.py 문제는 명령의 원본을 살펴봄으로써 해결할 수 있습니다.

+0

옙, 나 자신을 알아 냈으니 대답으로 업데이트하겠습니다. – Derek