0

How I can insert json values that I am getting from request.get("some url") into database in django?

Pradip Kachhadiya
  • 2,067
  • 10
  • 28
sqty87
  • 1
  • 1

1 Answers1

0

You can use django-json-field or insert json data in TextField.

For ex.

models.py:

import json

class MyModel(models.Model):
    ...
    other_detail = models.TextField(default="{}")

    @property
    def other_detail_json(self):
        return json.loads(self.other_detail)

views.py:

# Insert JSON data
...
data = request.get("some url")
other_details = json.dumps(data)
MyModel.objects.create(other_detail = other_details,...)

#fetch JSON data
obj =  MyModel.objects.get(id = id)
other_detail = obj.other_detail_json
print(other_detail)
Pradip Kachhadiya
  • 2,067
  • 10
  • 28