AUTH_USER_MODEL do not fulfill db correctly

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:


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

settings.py:

AUTH_USER_MODEL = 'my_app_only_for_Users_&_Groups.User'

my_other_app.models.py:

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 :frowning: 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:

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

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 :slight_smile:

The perfect and only example from which I understood this is:
solution - mannually assign current User to field