If you are using Django REST framework on serving your APIs, you probably did the below
code in returning the related object in your serializer.
class MyTableSerializer(serializers.ModelSerializer):
user = UserSerializer(many=False, read_only=True)
class Meta:
fields = '__all__'
model = MyTable
But doing this will not allow your API to pass the foreign key id. Instead, you need to include the field name in the serializer like the code below:
class MyTableSerializer(serializers.ModelSerializer):
user = UserSerializer(many=False, read_only=True)
user_id = serializers.IntegerField(write_only=True)
class Meta:
fields = '__all__'
model = MyTable
Comments
Rafael (not verified)
• 1 year ago• PermalinkThanks!
It works!
Phillip (not verified)
• 1 year ago• PermalinkThanks
This has saved me alot of pain. Thank you
Anonymous (not verified)
• 1 year ago• PermalinkThanks
Saved me a bit of time
Talha (not verified)
• 11 months ago• PermalinkThanks!
Thanks a lot!
Olga (not verified)
• 10 months ago• PermalinkThank! It looks like it is…
Thank! It looks like it is what I was looking for!
FoxHung (not verified)
• 10 months ago• PermalinkPython
Nice
Somangshu (not verified)
• 9 months ago• PermalinkThank
You saved the day, Simple to implement and understand, Although the create method had to be updated. Thanks again
Mohammad (not verified)
• 9 months ago• PermalinkThanks
Thank you you very much. I had been searching for this for two days
Nicolas (not verified)
• 9 months ago• PermalinkYou rock Mark Thank you a…
You rock Mark
Thank you a lot!
Patrick Kennedy (not verified)
• 8 months ago• PermalinkNoice. Thanks. Life saver.
Noice. Thanks. Life saver.
DIEGO ARRIETA (not verified)
• 6 months ago• PermalinkTTTTTTTT HHHHHHH AAAAAAAA…
TTTTTTTT
HHHHHHH
AAAAAAAA
NNNNNNNNN
KKKKKKKKKKK
SSSSSSSSSSSS
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Leo (not verified)
• 4 months ago• PermalinkI literally wasted 2 hours…
I literally wasted 2 hours before finding this. Thank you!!!!
Add new comment