How to save model object using only foreign keys id in django-rest-framework
sium_hossain
Posted on June 9, 2022
If you want save some data like me in a model which has foreign key field and want to save only by id in foreign key field. Then this method is for you 🐎
models.py
Here is the foreign key field where I want to save this object just passing foreign key id.
In serializers.py
we have to put serializers.PrimaryKeyRelatedField
on foreign key field name like this -
unit = serializers.PrimaryKeyRelatedField(queryset= Unit.objects.all(),many=False)
💖 💪 🙅 🚩
sium_hossain
Posted on June 9, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.