# Model not updating to the database

**URL:** <https://forum.djangoproject.com/t/model-not-updating-to-the-database/12105>\
**Category:** Mystery Errors\
**Created:** [February 12, 2022, 10:18am UTC](https://forum.djangoproject.com/t/model-not-updating-to-the-database/12105 "2022-02-12T10:18:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maxx-Yo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/maxx-yo/32/7419_2.png) [@Maxx-Yo](https://forum.djangoproject.com/u/Maxx-Yo)\
**Post date:** [February 12, 2022, 10:18am UTC](https://forum.djangoproject.com/t/model-not-updating-to-the-database/12105/1 "2022-02-12T10:18:10Z")

</div>

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.

```auto
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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/d/d5cc296e064538876d65aa6923f95cd3d2026c0a.png)

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 12, 2022, 8:08pm UTC](https://forum.djangoproject.com/t/model-not-updating-to-the-database/12105/2 "2022-02-12T20:08:18Z")

</div>

How are you creating this user model? How are you populating the data in the UserAccess model?  
(Can you post the views responsible for this?)

---

<div class="post-metadata">

**Author:** ![Maxx-Yo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/maxx-yo/32/7419_2.png) [@Maxx-Yo](https://forum.djangoproject.com/u/Maxx-Yo)\
**Post date:** [February 13, 2022, 5:37am UTC](https://forum.djangoproject.com/t/model-not-updating-to-the-database/12105/3 "2022-02-13T05:37:57Z")

</div>

I’ve got it fixed, it seems somehow the Models is only saved when changes is detected, so when I’m specifying all known default values, the Model won’t proceed to save.

I’ve changed the CharField to have a default of None

```auto
    access_status = models.CharField(max_length=11, choices=ACCESS_STATUS_CHOICES, default=None)

```

---

<div class="post-metadata">

**Author:** ![Maxx-Yo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/maxx-yo/32/7419_2.png) [@Maxx-Yo](https://forum.djangoproject.com/u/Maxx-Yo)\
**Post date:** [February 13, 2022, 5:38am UTC](https://forum.djangoproject.com/t/model-not-updating-to-the-database/12105/4 "2022-02-13T05:38:52Z")

</div>

I was not using any Views on creating a user, as I’m using Djoser to handle User Authentication Endpoints, but I got the problems solved, Thanks.
