Reputation: 11
I am pulling in some JSON data from a external API.
{
"id": 1,
"body": "example json"
},
{
"id": 2,
"body": "example json"
}
my User model:
class User(models.Model):
body = models.CharField(max_length=200)
how i can save the json response into my model ?
Upvotes: 1
Views: 1914
Reputation: 454
Save a model defined object by using the Model API
import json
json_result = '''{
"id": 2,
"body": "example json"
}'''
data = json.loads(json_result) # first convert to a dict
id = data.get("id") # get the id
body = data.get("body") # get the body
user = User.objects.create(id=id, body=body) # create a User object
user.save() # save it
Upvotes: 2