OneToOneField value must be a model instance, not a value?

28 views
Skip to first unread message

regtech

unread,
Jan 2, 2019, 11:12:09 AM1/2/19
to django-users
Hello everyone!
I am a junior who is first using OneToOneField, I found that the value which is giving to the field only could be a instance of the related model, can't be a value of the field which is specifed by the "to_field" ( else django will raise excption ).
In the other word, "to_field" does not make effect, django performing OneToOneField use PK.
I feel incomprehension and it makes me trouble on working.
Thanks!

Kuber Sodari

unread,
Jan 2, 2019, 11:17:57 AM1/2/19
to Django users
From official documentation "to_field is the field on the related object that the relation is to. By default, Django uses the primary key of the related object. If you reference a different field, that field must have unique=True." May be read it once, you will get some clearance.
I didn't quite understand your use-case though. Can you explain it, which field are you using OneToOneField for and why?
Reply all
Reply to author
Forward
0 new messages