ValueError when assigning value to ForeignKey field

Django version 5.1.1
Python 3.12.4

Problem:
When trying to add a new record to ToLog model using absolute values I catch an error when setting a value to the foreignkey field 'noc_tech_name '. I do know that the foreignkey field 'noc_tech_name ’ saves the selection as an integer.
In the past when I have banged through issues and presented my code I was chastised of how convoluted or bad it was and should of done it another way, so I’m asking for help before I waste everyone’s time. Thank you.

Overview:
I have a foreignkey relationship between class ToLog(models.Model) and Django User model as seen below. When adding a record using a form and select input it works fine. But I also need to be able to copy record field values from previous records and create new ones not using a form. When I try this it catches an error when trying to set 'noc_tech_name = username’ . I have tried many things and I’m at a loss of how to solve this.

I need to solve this as I will have many other instances of this throughout the project.

error thrown:
Causes ValueError: Cannot assign “‘admin’”: “ToLog.noc_tech_name” must be a “User” instance.

views.py (partial)

user = request.user
username = user.username 
        
p = ToLog.objects.create(
        social_title = ivanti_incs.IvantiSearchVal,
        noc_tech_name = username,                  
            )    

This line above 'noc_tech_name = username, ’ causes the error

models.py (partial)

noc_tech_name = models.ForeignKey(User, on_delete=models.DO_NOTHING, blank=True, null=True)

This is a reference to a Model, not a field in the model.

Therefore, what you want to assign to that field (noc_tech_name) is a reference to the instance, not any field within that instance.
e.g. noc_tech_name = request.user

That’s an internal implementation detail that you generally don’t need to worry about or account for. (It’s also not universally true.) It’s one of those details you can ignore unless you run into a situation where you need to know about it.

Yes, it is actually hinting you about the solution. Check it out:

Apparently you defined a model called ToLog and one of its fields (noc_tech_name) points to User via a ForeignKey

you should do the following instead:

from wherever import User
# Take note of above line. You already know where you defined User.
# wherever represents the module bearing the User class

user = User.objects.get(pk=1)
# Above will get the instance of user from database using the primary key of 1

p = ToLog.objects.create(
    social_title = ivanti_incs.IvantiSearchVal,
    noc_tech_name = user,                  
)

The fundamental problem is that this variable name is a lie. It’s not a name. It’s a User. If I write your name on a piece of paper, that paper can’t eat food or drink water. The name is not the same as the thing itself.

noc_tech_name should be renamed! Probably to noc_tech.

Thank you so much onyeibo.
I used what u taught me to build out the final code that worked. Really everyone who replied was of great help. It brought it all together for me. I notice that you guys also try to make me think and learn all of this on a deeper level. I really appreciate it. Maybe some day I will be helpful like this.

2 Likes