# AUTH\_USER\_MODEL do not fulfill db correctly

**URL:** <https://forum.djangoproject.com/t/auth-user-model-do-not-fulfill-db-correctly/24863>\
**Category:** Forms & APIs\
**Created:** [October 28, 2023, 7:46pm UTC](https://forum.djangoproject.com/t/auth-user-model-do-not-fulfill-db-correctly/24863 "2023-10-28T19:46:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![woojci](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/woojci/32/6649_2.png) [@woojci](https://forum.djangoproject.com/u/woojci)\
**Post date:** [October 28, 2023, 7:46pm UTC](https://forum.djangoproject.com/t/auth-user-model-do-not-fulfill-db-correctly/24863/1 "2023-10-28T19:46:15Z")

</div>

Hello,  
I have a problem with custom User model. Maybe take a look into my code and then everything will be clear:

my\_app\_only\_for\_Users\_&\_Groups.models.py:

```auto

class User(AbstractUser):
    phonenumber= models.PositiveIntegerField(blank=True, null=True)

```

settings.py:

```auto
AUTH_USER_MODEL = 'my_app_only_for_Users_&_Groups.User'

```

my\_other\_app.models.py:

```auto
from django.conf import settings

class ProductionCheck(models.Model):
    many_fields....
    created_by= models.ForeignKey(settings.AUTH_USER_MODEL,
                                   on_delete=models.SET_NULL,
                                    null=True)

```

The problem is that after save() new object I have blank cell in my datebase ☹ When I try to check- print(settings.AUTH\_USER\_MODEL) the current user in views.py I only see in console the string which I assigned to AUTH\_USER\_MODEL so in my case my\_app\_only\_for\_Users\_&\_Groups.User.

I would like to ask for explaination of this situation. After check many tutorials, with the same code I hope that I will get in my database’s cell something like string of User who created the object or its ID.

Using the ForeingKey in models.py is not enouth to achieve my expectation?  
Should I use everytime something like that in my views.py:

```auto
def form_valid(self, form):
        form.instance.created_by= self.request.user

```

---

<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:** [October 28, 2023, 11:45pm UTC](https://forum.djangoproject.com/t/auth-user-model-do-not-fulfill-db-correctly/24863/2 "2023-10-28T23:45:15Z")

</div>

> [@woojci](#):
>
> ```auto
> def form_valid(self, form):
> form.instance.created_by= self.request.user
> 
> ```

This is not how you update the model being created by a form.

We can’t answer how this needs to be done without seeing the complete form and the view.

In the general case, you need to save the form with `commit=False` to get the instance of the object being save. Then you update _that_ object and then save the modified object.

See [Creating forms from models | Django documentation | Django](https://docs.djangoproject.com/en/4.2/topics/forms/modelforms/#the-save-method)

---

<div class="post-metadata">

**Author:** ![woojci](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/woojci/32/6649_2.png) [@woojci](https://forum.djangoproject.com/u/woojci)\
**Post date:** [October 30, 2023, 9:11am UTC](https://forum.djangoproject.com/t/auth-user-model-do-not-fulfill-db-correctly/24863/3 "2023-10-30T09:11:23Z")

</div>

I made the mistake of thinking that there was magic in Django and it wasn’t up to me to manually assign the currently logged in user to a specific object in a function in views.py. Unfortunately, there is no change on the Internet that when declaring a field using ForeignKey and settings.AUTH\_USER\_MODEL you have to manually assign this user because the system will not assign it itself. I simply supplemented my uncertainty in this aspect with the magic of Django… The tutorials lacked this simple reminder that we only indicate the user class, but you still have to assign it yourself in the code 🙂

The perfect and only example from which I understood this is:  
[solution - mannually assign current User to field](https://stackoverflow.com/questions/67752800/assign-username-to-field-in-models-py-table)
