Foreign key relation without pk #8603
Unanswered
JasperAlexander
asked this question in
Question & Answer
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
How to make a foreign key relation using a field other than the primary key with a post request? For example, if you have users and items, how to make a new item with a relation to an existing user with only the username of the user, not the id (primary key)?
models.py
serializers.py
Adding the following code to the ItemSerializer seems to allow me to add a new item with a relation to an existing user by providing the id (primary key) of a user in a post request. Changing owner_id to owner_username gives me a TypeError: 'Item() got unexpected keyword arguments: 'owner_username''.
Beta Was this translation helpful? Give feedback.
All reactions