Skip to content
Advertisement

How to send JSON format data in postman to django models that have a foreign key to another model?

I have tried to send a POST request to django views.py file using postman. It was working when I sent a POST and GET request to django models that haven’t ForeignKey field. However, when I tried to send a POST request to a django model that have a ForeignKey field, it wasn’t working. My question is how to send a JSON format data using postman to django models that have a Foregin Key field. The models are as follows:

JavaScript

I have tried in this way (in postman):

JavaScript

As you can see the articleId is a foreignKey field. Here is the output:

JavaScript

Advertisement

Answer

Django models have id field by default so the primary key of the models will be the id. if you use foreign key in Django model, it references the primary key of the referenced model. Here what you face is, you are referencing non existing id – meaning wrong id, or, no instance of article exist with that id.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement