1

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 ?

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.