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

 

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 “User.pk always returns User.id.” 
It doesn’t matter how I write it.

 

user_id = User.pk 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 User.id is in a template where I get the 
following results.

 

Template Tags

{{user.pk}} = user.id

{{user.id}} = user.id

{{user.username}} = username

using instance as object in situations like signals.  

instance.id = user.id

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 
p_user_id=instance.id. 

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 instance.id. 
Error:p_user_id must be a User instance. 

p_user_id = instance.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 User.pk or User.id 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. 


Thanks, 

Blaine
 

-- 
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 django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/8a4f8592-e341-42c6-9be0-7bc7efe83ae2n%40googlegroups.com.

Reply via email to