Below is my Extended User Model by using the One to One Field Connection, I have assigned a default value for all the values so that values are assigned automatically when users create their account.
Problem: The User Access Model below is not updated to the database when new user is created. The default values can be seen in the Django Admin Panel, but can’t be accessed from the Database, as it seems it does not update the User Access table.
def generate_unique_user_id():
while True:
unique_user_id = random.randint(10000,200000000)
if UserAccess.objects.filter(unique_user_id=unique_user_id).count() == 0:
break
return unique_user_id
def profile_pic_path(instance, filename):
ext = filename.split('.')[-1]
uuid = shortuuid.uuid()
filename = f"{uuid}.{ext}"
return os.path.join('UserProfilePhoto/', filename)
# Create your models here.
class UserAccess(models.Model):
ACCESS_STATUS_CHOICES = [
('CUSTOMER', 'Customer'),
('STAFF', 'Staff'),
('SUPER-USER', 'Super-User'),
('ADMIN', 'Admin'),
]
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='useraccess')
unique_user_id = models.IntegerField(default=generate_unique_user_id, unique=True, primary_key=True)
access_level = models.IntegerField(default=1, validators=[MinValueValidator(1), MaxValueValidator(15)])
access_status = models.CharField(max_length=11, choices=ACCESS_STATUS_CHOICES, default='CUSTOMER')
profile_pic = models.ImageField(null=True, blank=True, unique=True, upload_to=profile_pic_path)
def __str__(self):
return self.unique_user_id
My Admin Page Displays the default value but the database does not consists of the information, it seems that the data here is not saved to the database.