why don't you try to return it by context using a dictionary?, specify
where what is the key and what is the value

El mié, 23 de nov. de 2022 10:19 a. m., Blaine Wimberly <
[email protected]> escribió:

> 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.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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/2c973cff-1b66-4e5d-9f69-e5a83077dfa2n%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/2c973cff-1b66-4e5d-9f69-e5a83077dfa2n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHsGuW%3Dxawujseg2c3ehnB0fy8ehzUcjJTc-Y8Fp3i9AuJpFXg%40mail.gmail.com.

Reply via email to