개발일지?/내배캠

캠프 7주 3일차 TIL

규파팡 2023. 4. 27. 00:04

오늘

오류를 만났는데 너무 간단했던 것과 근본적 원일을 찾은 오류가 있었다.

간단한 건 경로오류였기에 pass!

큰 오류는 age를 IntegerField로 작성했더니 super계정부터 오류가 발생했다. 이 부분은 후발대 강의시간에 튜터님 도발생했던 오류였는데 위치를 못 잡아서 해결을 못하다가 결국 튜터님께 질문하고 해결했다

처음에 오류를 만났을 때 나는 이게 왜 오류가나는지메시지를 보고 int()인걸 보고 왜 심화강의때했던 거랑 같은데 오류가 날까? 싶었는데 저번에 django프로젝트에서는 CharField로 작성했고 이번엔 IntegerField로 작성했으니깐 당연한 결과였다...ㅠㅠㅠㅠ 그래서 저부분을 CharField로 수정하면 오류가 안 날 테지만 그래도 나이니깐 숫자로 입력하고 싶었다 결국 super계정에서는 이메일과 비밀번호만 받기 때문에 오류가난 거고 그럼 super계정 부분을 커스텀해주면 해결될 거라 생각했다. 때마침 super계정 커스텀 부분이 있었다

class UserManager(BaseUserManager):
    def create_user(self, email, password=None, **kwarg):
        """
        Creates and saves a User with the given email and password.
        """
        if not email:
            raise ValueError("Users must have an email address")

        user = self.model(
            email=self.normalize_email(email),
            **kwarg,
        )

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

    def create_superuser(self, email, password=None):
        """
        Creates and saves a superuser with the given email and password.
        """

        user = self.create_user(
            email,
            age=0,
            password=password,
            
        )
        user.is_admin = True
        user.save(using=self._db)
        return user

길지만 해결은 간단했다 위부터 보면

    def create_user(self, email, password=None, **kwarg):

keywords arguments를 추가해 주고

user = self.model(
    email=self.normalize_email(email),
    **kwarg,
)

 

user부분에도 keywords arguments를 입력해 준다. 그리고 age를 super계정에서 직접 사용하지 않기 때문에 입력하게 작업해 줘도 되지만 임의값으로 넣어줬다..

    def create_superuser(self, email, password=None):
        """
        Creates and saves a superuser with the given email and password.
        """

        user = self.create_user(
            email,
            age=0,
            password=password,
            
        )
        user.is_admin = True
        user.save(using=self._db)
        return user
user = self.create_user(
    email,
    age=0,
    password=password,
)

이 부분이다 age=0으로 작성해 주고 저장했다 다른 방법으로는 필수값이 아니게 null=True를 해줘도 된다. 아무리 해도 오류가 나서 뭐지? 했는데 migration을 안 해줬었다 ㅠㅠㅠㅠ

 

오늘의 규파팡이 느낀 점!

과제를 하면 성장이 빨 라지는 것 같다. 오늘원래 다른 걸로 til을 쓰고 싶었는데 오랜만에 해결할 맛 나는 오류를 만났기에 오류로 작성했다.

TIL 대표이미지