Here's my custom user model:
class Account(AbstractBaseUser):
email = models.EmailField(unique=True, max_length=255)
firstname = models.CharField(max_length=40)
lastname = models.CharField(max_length=40)
date_joined = models.DateTimeField(auto_now_add=True)
is_active = models.BooleanField(default=True)
is_verif = models.BooleanField(default=)
is_superuser = models.BooleanField(default=False)
USERNAME_FIELD = "email"
REQUIRED_FIELDS = ["firstname", "lastname"]
objects = AccountManager()
def __str__(self):
return self.email
#property
def is_staff(self):
return self.is_superuser
#property
def is_admin(self):
return self.is_superuser
def has_perm(*args, **kwargs):
return True
def has_module_perms(*args, **kwargs):
return True
So right now I have a standard djoser account verification system. So I'm unable to login with unverified user because the is_active field is set to False.
Where and how do I modify the code so that every time I verify an account it checks the is_verif field instead of the is_active and the is_active field is always set to True ?
Thank you
You can replace the is_active field with a methods named is_active with the #property decorator and inside that method return the is_verif value.
#property
def is_active(self):
return self.is_verif
But this solution works only if you don't need the is_active field.
Related
I tried to put this code to deactivate the sensitivity of the characters when logging in, but it does not work. What is the problem with it? It still appears to me. Please consider the sensitivity of the characters
I want to use this method. I do not want to use the backend method
Please help me find the problem
class CustomUserManager(UserManager):
def get_by_natural_key(self, username):
return self.get(username__iexact=username)
class User(AbstractBaseUser):
"""
Decapolis main User model
"""
username = models.CharField(max_length=40, unique=True)
email = models.CharField(max_length=40, unique=True)
first_name = models.CharField(_('first name'), max_length=30, blank=True)
last_name = models.CharField(_('last name'), max_length=30, blank=True)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
company = models.ForeignKey(settings.COMPANY_MODEL, on_delete=models.CASCADE, related_name='user_company',
null=True, blank=True)
roles = models.ManyToManyField(Role, blank=True)
objects = CustomUserManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']
def has_role(self, role_name):
"""
Check user has role
:param role_name: role name to check
:type role_name: str
:return: True or False
:rtype: bool
"""
return True if role_name in self.roles else False
def has_perm(self, perm, obj=None):
"""
Does the user have a specific permission?
Not implemented yet
:param perm: permission to check
:type perm: str
:param obj: object to check
:type obj:
:return:
:rtype:
"""
# Simplest possible answer: Yes, always
return True
def has_module_perms(self, app_label):
"""
Does the user have permissions to view the app `app_label`?
Not implemented yet
:param app_label:
:type app_label:
:return:
:rtype:
"""
# Simplest possible answer: Yes, always
return True
def get_full_name(self):
"""
Returns the first_name plus the last_name, with a space in between.
"""
full_name = '%s %s' % (self.first_name, self.last_name)
return full_name.strip()
def __str__(self):
return self.username
i have already Django project that completely works fine with custom user model phone and password for registering and sign up,
here i need to add firebase phone number authentication to send sms to user before register to my site ???
prettier any help here .
my custom user model
class MyUser(AbstractBaseUser):
email = models.EmailField(blank=True,null=True, verbose_name='Email Address',max_length=255 )
name = models.CharField(max_length=100,verbose_name= 'Username', )
last_name = models.CharField(blank=True,max_length=100,verbose_name= 'Family Name')
mobile = models.IntegerField(unique=True,verbose_name= 'Mobile Number')
governorate = models.CharField(blank=True,null=True,verbose_name='Governorate',max_length=255)
image =models.ImageField(blank=True,upload_to='profile_pics', default='profile_pics/avatar.png')
Is_Banned = models.BooleanField(default=False)
notification =models.BooleanField(default=False,verbose_name= 'Enable Notification')
language_choices =(
("Arabic", "Arabic"),
("English", "English"),
("Kurdish", "Kurdish"),
)
language = models.CharField( choices = language_choices,blank=True, default = 'Arabic',max_length=50,verbose_name= 'Language')
is_admin = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
objects = MyUserManager()
USERNAME_FIELD = 'mobile'
REQUIRED_FIELDS = ['name','email']
def __str__(self):
return self.name
class Meta:
verbose_name_plural='Users'
def Adressess(self):
return 'adressess'
def has_perm(self, perm, obj=None):
return self.is_admin
def has_module_perms(self, app_label):
return True
#property
def is_staff(self):
return self.is_admin
#property
def first_name(self):
return self.name
When I migrate the Django project model, I get the error:
there is no unique constraint matching given keys for referenced table "accounts_account
my models is:
class Account(AbstractBaseUser):
email = models.EmailField(verbose_name='ایمیل', max_length=60, unique=True)
username = models.CharField(verbose_name='نام کاربری', max_length=30, unique=True)
phone_number = models.CharField(max_length=11, blank=True, null=True, default="")
profile_image = models.ImageField(default="profile.jpg", upload_to='profile/images')
# media/blog/images/img1.jpg
...
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
objects = MyAccountManager()
def __str__(self):
return self.email
def has_perm(self, perm, obj=None):
return self.is_admin
def has_module_perms(self, app_label):
return True
in pgAdmin the Primary Key is not changeable
I created a custom user model using the AbstractBaseUser class (see source code below). However, after realising that the column I needed in the table should be called "is_active" not "active", I renamed the column in the models file, however, the migrations only removed the "active" column but didn't create an "is_active" column. Any ideas?
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
first_name = models.CharField(max_length=50, null=True)
last_name = models.CharField(max_length=100, null=True)
is_active = models.BooleanField(default=True)
staff = models.BooleanField(default=False) # a admin user; non super-user
date_joined = models.DateTimeField(auto_now_add=True)
manager_access = models.BooleanField(default=False)
USERNAME_FIELD = 'email'
# Email & Password are required by default.
REQUIRED_FIELDS = ['first_name', 'last_name']
objects = UserManager()
def get_full_name(self):
return self.first_name + self.last_name
def __str__(self):
return self.email
def has_perm(self, perm, obj=None):
"Does the user have a specific permission?"
# Simplest possible answer: Yes, always
return True
def has_module_perms(self, app_label):
"Does the user have permissions to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True
#property
def is_staff(self):
"Is the user a member of staff?"
return self.staff
#property
def is_admin(self):
"Is the user a admin member?"
return self.is_superuser
#property
def is_active(self):
"Is the user active?"
return self.is_active
I ended up removing this chunk of code:
#property
def is_active(self):
"Is the user active?"
return self.is_active
After doing this and rerunning the migrations, the is_active column was added.
Trying to make a password reset view. I'm using the auth_views built in views ie PasswordResetView and PasswordResetConfirmView for resetting password. However I was getting this error
Cannot resolve keyword 'is_active' into field. Choices are: active, admin, email, first_name, id, last_login, last_name, logentry, password, staff, timetables
. Tried changing active to is_active and getting this error.
django.core.exceptions.FieldError: Unknown field(s) (active) specified for User
Not able to make migrations
models.py
class User(AbstractBaseUser):
first_name = models.CharField(max_length=50, blank=True, null=True)
last_name = models.CharField(max_length=50, blank=True, null=True)
email = models.EmailField(max_length=254, unique=True)
is_active = models.BooleanField(default=True)
staff = models.BooleanField(default=False)
admin = models.BooleanField(default=False)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = UserManager()
def __str__(self):
return self.email
def get_first_name(self):
if self.first_name:
return self.first_name
return self.email
def get_last_name(self):
return self.last_name
def has_perm(self, perm, obj=None):
return True
def has_module_perms(self, app_label):
return True
#property
def is_staff(self):
return self.staff
#property
def is_admin(self):
return self.admin
#property
def is_active(self):
return self.is_active
forms.py
class UserAdminChangeForm(forms.ModelForm):
"""A form for updating users. Includes all the fields on
the user, but replaces the password field with admin's
password hash display field.
"""
password = ReadOnlyPasswordHashField()
class Meta:
model = User
fields = ('email', 'first_name', 'last_name',
'password', 'active', 'admin')
def clean_password(self):
# Regardless of what the user provides, return the initial value.
# This is done here, rather than on the field, because the
# field does not have access to the initial value
return self.initial["password"]
Try to change is_active = models.BooleanField(default=True) to active = models.BooleanField(default=True) in your User class, and is_active property
see below
class User(AbstractBaseUser):
first_name = models.CharField(max_length=50, blank=True, null=True)
last_name = models.CharField(max_length=50, blank=True, null=True)
email = models.EmailField(max_length=254, unique=True)
active = models.BooleanField(default=True)
staff = models.BooleanField(default=False)
admin = models.BooleanField(default=False)
#property
def is_active(self):
return self.active