How to save a json object in django

Question:

i am trying to develop a system which displays a list of experts in a topic when that particular topic is searched. The search function is working and the list of experts (result) is generated from an API which is in form of a list of json objects as shown below:

result={
        "experts":[
  {
    "thumbnail": "https://scholar.googleusercontent.com/citations?view_op=small_photo&user=JicYPdAAAAAJ&citpid=2",
    "name": "Geoffrey Hinton",
    "link": "https://scholar.google.com/citations?hl=en&user=JicYPdAAAAAJ",
    "author_id": "JicYPdAAAAAJ",
    "email": "Verified email at cs.toronto.edu",
    "affiliations": "Emeritus Prof. Comp Sci, U.Toronto & Engineering Fellow, Google",
    "cited_by": 638900,
    "interests": [
      {
        "title": "machine learning",
        "serpapi_link": "https://serpapi.com/search.json?engine=google_scholar_profiles&hl=en&mauthors=label%3Amachine_learning",
        "link": "https://scholar.google.com/citations?hl=en&view_op=search_authors&mauthors=label:machine_learning"
      },
      {
        "title": "psychology",
        "serpapi_link": "https://serpapi.com/search.json?engine=google_scholar_profiles&hl=en&mauthors=label%3Apsychology",
        "link": "https://scholar.google.com/citations?hl=en&view_op=search_authors&mauthors=label:psychology"
      },
      {
        "title": "artificial intelligence",
        "serpapi_link": "https://serpapi.com/search.json?engine=google_scholar_profiles&hl=en&mauthors=label%3Aartificial_intelligence",
        "link": "https://scholar.google.com/citations?hl=en&view_op=search_authors&mauthors=label:artificial_intelligence"
      },
      {
        "title": "cognitive science",
        "serpapi_link": "https://serpapi.com/search.json?engine=google_scholar_profiles&hl=en&mauthors=label%3Acognitive_science",
        "link": "https://scholar.google.com/citations?hl=en&view_op=search_authors&mauthors=label:cognitive_science"
      },
      {
        "title": "computer science",
        "serpapi_link": "https://serpapi.com/search.json?engine=google_scholar_profiles&hl=en&mauthors=label%3Acomputer_science",
        "link": "https://scholar.google.com/citations?hl=en&view_op=search_authors&mauthors=label:computer_science"
      }
    ]
  },
  {
    "thumbnail": "https://scholar.googleusercontent.com/citations?view_op=small_photo&user=kukA0LcAAAAJ&citpid=3",
    "name": "Yoshua Bengio",
    "link": "https://scholar.google.com/citations?hl=en&user=kukA0LcAAAAJ",
    "author_id": "kukA0LcAAAAJ",
    "email": "Verified email at umontreal.ca",
    "affiliations": "Professor of computer science, University of Montreal, Mila, IVADO, CIFAR",
    "cited_by": 605714,
    "interests": [
      {
        "title": "Machine learning",
        "serpapi_link": "https://serpapi.com/search.json?engine=google_scholar_profiles&hl=en&mauthors=label%3Amachine_learning",
        "link": "https://scholar.google.com/citations?hl=en&view_op=search_authors&mauthors=label:machine_learning"
      },
      {
        "title": "deep learning",
        "serpapi_link": "https://serpapi.com/search.json?engine=google_scholar_profiles&hl=en&mauthors=label%3Adeep_learning",
        "link": "https://scholar.google.com/citations?hl=en&view_op=search_authors&mauthors=label:deep_learning"
      },
      {
        "title": "artificial intelligence",
        "serpapi_link": "https://serpapi.com/search.json?engine=google_scholar_profiles&hl=en&mauthors=label%3Aartificial_intelligence",
        "link": "https://scholar.google.com/citations?hl=en&view_op=search_authors&mauthors=label:artificial_intelligence"
      }
    ]
  }
        ]
    }

Now this "result" object is directly passed to an appropriate html template.Each "expert" in the "experts" list is iterated and displayed in the html page as shown:
results page

I want the user to be able to save the data of each individual expert.So I added a "Save" button beside each expert result.At the moment I cannot figure out out how to get all the data of an "expert" json object when the user clicks on the "Save" button beside each result.How do I create a URL pattern that can get the entire "expert" json object when the user clicks on the "Save" button and this json object can then be stored in database?

Asked By: SIM777

||

Answers:

You can make a view that receive the expert id and the info you want to pass, and in it you can save the info to the db.

Steps:

  • The href attr of the ‘Save’ will be as the following:
    {% url 'your_view_name' expert.id %}
  • Override the post method of your view.
  • In the post method you will get expert.id parameter, get the expert info then update the db.

The tricky part in those steps is how to get the expert data from its id?
You can request the api another time and get this expert info then save them as you want, OR to reduce the calls of the api and make them one call for each cycle you can use caching for the response by redis for example or you can store the responses of the api in your db.

Answered By: Waseem Kn
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.