Why can't I change primary key in postgresql? - django

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

Related

Django Firebase Phone Number Auth

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

Django Migrations not detecting is_active column

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.

Cannot resolve keyword 'is_active' into field

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

is_active is not changing when custom user logged out

I am working on custom user model, but the logout feature is not synchronized with root password. is_active(here activated) variable is not changing to False when I logout and the superuser logged in Django admin gets logout when logout button is pressed in the front end user. How to fix this.
models.py
class Users(AbstractBaseUser, PermissionsMixin):
objects = UserManager()
mobile_no = models.IntegerField(_('MobNumber'), null=True, blank=True,unique=True)
email = models.EmailField(_('Email'), max_length=75, null=False, blank=False)
first_name = models.CharField(_('FirstName'), max_length=50, null=True, blank=True)
last_name = models.CharField(_('LastName'), max_length=70, null=True, blank=True)
role = models.CharField(_('Role'), max_length=70, null=True, blank=True)
location = models.CharField(_('Location'), max_length=70, null=True, blank=True)
date_time = models.DateTimeField(_('DateTime'), auto_now=True, null=True, blank=True)
activated = models.BooleanField(_('activated'), default=False)
is_admin = models.BooleanField(_('is_admin'), default=False)
is_itstaff = models.BooleanField(_('is_staff'), default=False)
def __unicode__(self):
return str(self.mobile_no)
def __str__(self):
return str(self.mobile_no)
def get_full_name(self):
return self.first_name + " " + self.last_name
class Meta:
ordering = ['-id']
#property
def is_staff(self):
return self.is_admin
def has_perm(self, perm, obj=None):
return self.is_admin
def has_module_perms(self, app_label):
return self.is_admin
USERNAME_FIELD = 'mobile_no'
REQUIRED_FIELDS = ['role']
views.py
#login_required
def user_logout(request):
logout(request)
return HttpResponseRedirect(reverse('login'))

django expected an indented block

I am getting error in line 71
class UserProfile(AbstractBaseUser):
name = models.CharField(max_length=25)
email = models.EmailField(max_length=75)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = MyUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['name']
def get_full_name(self):
# The user is identified by their email address
return self.email //line error
def get_short_name(self):
# The user is identified by their email address
return self.email
def __str__(self): # __unicode__ on Python 2
return self.email
#property
def is_staff(self):
"Is the user a member of staff?"
# Simplest possible answer: All admins are staff
return self.is_admin
This is error expected an indented block in "return"
return self.email
How can I solve this problem?
The whole class definition must be indented, like this:
class UserProfile(AbstractBaseUser):
name = models.CharField(max_length=25)
email = models.EmailField(max_length=75)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = MyUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['name']
def get_full_name(self):
# The user is identified by their email address
return self.email //line error
def get_short_name(self):
# The user is identified by their email address
return self.email
def __str__(self): # __unicode__ on Python 2
return self.email
#property
def is_staff(self):
"Is the user a member of staff?"
# Simplest possible answer: All admins are staff
return self.is_admin