사용자 정의 사용자 모델 (AbstractBaseUser
메소드 사용)을 사용하는 앱을 만들고 있습니다. 모든 관련 사용자 생성 양식을 작성하고 사용자가 로그인 할 수있는 방법을 제공했습니다. 그러나 현재 로그인 한 사용자 (예 : 계정 페이지 편집)의 정보가 필요한 페이지를 만들려고 할 때 문제가 생겼다. request.user
과 같은 여러 가지 방법을 시도했지만 아무 것도 잘 작동하지 않는 것 같습니다. 이 메서드를 사용하려고하면 TypeError가 발생합니다. " 'MyUser'개체는 인덱싱을 지원하지 않습니다."장고로 로그인 한 후 사용자 정보 가져 오기
참조 용으로 내 모델 클래스를 연결합니다.
모든 도움을 주시면 감사하겠습니다. 미리 감사드립니다.
models.py는
class MyUserManager(BaseUserManager):
def create_user(self, email, date_of_birth, password=None):
"""
Creates and saves a User with the given email, date of
birth and password.
"""
if not email:
raise ValueError('Users must have an email address')
user = self.model(
email=MyUserManager.normalize_email(email),
date_of_birth=date_of_birth,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, date_of_birth, password):
"""
Creates and saves a superuser with the given email, date of
birth and password.
"""
u = self.create_user(email=email,
password=password,
date_of_birth=date_of_birth
)
u.is_admin = True
u.save(using=self._db)
return u
class MyUser(AbstractBaseUser):
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
date_of_birth = models.DateField()
course = models.ForeignKey(Course, null=True)
location = models.ForeignKey(Location, null=True)
interests = models.ManyToManyField(Interest, null=True)
bio = models.TextField(blank=True)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = MyUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['date_of_birth']
스택 추적에서 예외가 발생하는 곳은 어디입니까? – mmdeas
views.py에서 한 줄의 코드입니다. 그 줄에는'u = MyUser.objects.get (request.user)'가 표시됩니다. – MrW