AttributeError:类型对象“ MyUser”没有属性“ USERNAME

编程入门 行业动态 更新时间:2024-10-25 23:23:30
本文介绍了AttributeError:类型对象“ MyUser”没有属性“ USERNAME_FIELD”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在django中构建一个自定义User类,以用于创建注册应用程序,每次尝试进行迁移时,我都会不断遇到上述错误。据我所知,我的代码是根据django文档此处 .. 我也将AUTH_USER_MODEL正确放置在我的设置配置中。 这是我的模型。py

I am building a custom User class in django to use in creating a signup application and I keep on getting the error above every time I try to makemigrations. As far as I can see, my code is per django documentation here.. I also have AUTH_USER_MODEL correctly placed in my settings configurations. Here's my models.py

`class MyUserManager(BaseUserManager): def create_user(self, email, first_name,last_name,profile_picture,phone_no,password=None): """ 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), first_name=first_name, last_name=last_name, profile_picture=profile_picture, phone_no=phone_no, ) user.set_password(password) user.save(using=self._db) return user def create_superuser(self, email, password): """ Creates and saves a superuser with the given email and password. """ SuperUser = self.create_user( email, password=password, ) SuperUser.staff = True SuperUser.admin = True SuperUser.save(using=self._db) return SuperUser class MyUser(AbstractBaseUser): email = models.EmailField( verbose_name = 'email_address', max_length=255, unique=True, # validators=email_validator, ) first_name = models.CharField(max_length=20,blank=False,null=False) last_name = models.CharField(max_length=20,blank=False,null=False) phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the format: '+254 ...'") phone_no = models.CharField(validators=[phone_regex], max_length=17, blank=False) profile_picture = models.ImageField(upload_to='media/',blank=False) # email_validator = EmailValidator(message='Invalid email # address',code=None,whitelist=None) is_active = models.BooleanField(default=True) is_staff = models.BooleanField(default=False) is_admin = models.BooleanField(default=False) objects = MyUserManager() USERNAME_FIELD = 'email' REQUIRED_FIELDS = ['first_name','last_name','phone_no','profile_picture'] # Email & Password are required by default def get_full_name(self): return self.email def get_short_name(): return self.email def __str__(self): return self.email def has_perm(self,perm,obj=None): #does user have a specific permission return True def has_module_pers(self,app_label): #does user have permissions to view the app 'app_label' return True @property def is_admin(self): return self.is_admin @property def is_active(self): return self.is_active # hook in the New Manager to our Model class MyUser(AbstractBaseUser): ... objects = MyUserManager() `

推荐答案

创建自定义用户模型

class User(AbstractUser): """User model.""" username = None email = models.EmailField(_('email address'), unique=True) USERNAME_FIELD = 'email' REQUIRED_FIELDS = []

CustomUser

您是:-扩展Django为User模型提供的基类。

You are: - Extending the base class that Django has for User models.

  • 删除用户名字段。
  • 使电子邮件字段必填且唯一。
  • 列出项目告诉Django,您打算将电子邮件字段用作 USERNAME_FIELD
  • 从REQUIRED_FIELDS 设置中删除电子邮件字段(它会自动作为USERNAME_FIELD包含在内)
  • Removing the username field.
  • Making the email field required and unique.
  • List itemTelling Django that you are going to use the email field as the USERNAME_FIELD
  • Removing the email field from the REQUIRED_FIELDS settings (it is automatically included as USERNAME_FIELD)

源链接

更多推荐

AttributeError:类型对象“ MyUser”没有属性“ USERNAME

本文发布于:2023-10-22 08:17:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1516964.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   对象   类型   AttributeError   MyUser

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!