Wednesday, November 23, 2022

Re: User model as ForeignKey forcing username instead of id.

Issue resolved.

In pure frustration, I dumped the database and deleted all migrations. 

While I am not positive, I am pretty sure the issue originated due to updating the original CharField to be the ForeignKey, Django in its infinite wisdom (not sarcastic) was finding the first non integer in the instance to assign to the foreignkey. 

In addition, in the admin panel for my profile model, it still shows the username in the dropdown, however, in the sql database the id is saved as an integer. 

As a reminder for those who may miss it like I did, when assigning a foreignKey Django adds a _id to the end of your foreignkeyfield when creating the column in the database. This can be problematic when creating raw sql queries as the field name in your model may be user_id, but in the database it is actually user_id_id making it myapp_model.user_id_id for raw queries instead of myapp_model.user_id. Just something to keep in mind. This does not impact things such as model.objects.user_id, as Django maps this correctly in the backend. 

Anyway, I appreciate the help all. :)

On Wednesday, November 23, 2022 at 8:22:14 AM UTC-6 Blaine Wimberly wrote:

For some reason, no matter what I do,, User (as ForeignKeyField, and just about anything else is forcing the username as the value instead of


I have googled searched this issue in every way I can think if. The only results I keep finding are the exact opposite issue where people are wanting the Primary Key to be something else other than id. On all of those issues I have found the answer was always " always returns" It doesn't matter how I write it.


user_id = print(user_id) result = username


user_id = models.OneToOneField(User, to_field='id', on_delete=models.CASCADE)

Result is = username


user_id = models.ForeignKeyField(User, to_field='id', on_delete=models.CASCADE)

Result is username


I have attempted to change the primary key in the admin_user table in my database to point to id, result was that 0 rows affected.


The only time I am able to get the is in a template where I get the following results.


Template Tags

{{}} =

{{}} =

{{user.username}} = username

using instance as object in situations like signals. =

instance = username

instance.username = (no result)

With the above use in a custom signal function I am able to create an entry in my profile table and populate p_username = instance and

In the above use, I can have p_username as a foreignkey to User, however, if I change it to instance.username I get an error when the signal function executes "p_username" must be a User instance. I get the same error when changing the ForeignKey to p_user_id and set it to Error:p_user_id must be a User instance. 

p_user_id = populates the p_user_id with the correct id as long as it isn't set as a foreignkey or one-to-one relationship on User. It just doesn't make sense. 

What I need is simply to be able to use the or as the foreignkey. Who the ___ uses username as the foreignkey for anything? I certainly can't imagine it being the ultimate key across an application where the user is the key to all things.


Anyway, any assistance would be greatly appreciated. Don't kill me for any typing errors, I have been working this one issue for 16 hours. My eyes are crossed. 




You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit

No comments:

Post a Comment