英文:
How to save a json object in django
问题
我正在尝试开发一个系统,当搜索特定主题时,显示该主题的专家列表。搜索功能正在工作,专家列表(结果)是从一个API生成的,其形式为下面显示的JSON对象列表:
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"
}
]
}
]
}
现在,这个"result"对象直接传递到一个适当的HTML模板中。"experts"列表中的每个"expert"都会被迭代并显示在HTML页面中,如下所示:
我希望用户能够保存每个专家的数据。因此,我在每个专家结果旁边添加了一个"保存"按钮。目前我无法弄清楚如何在用户点击结果旁边的"保存"按钮时获取"expert" JSON对象的所有数据。我应该如何创建一个URL模式,以在用户点击"保存"按钮时获取整个"expert" JSON对象,并将此JSON对象存储在数据库中?
英文:
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:
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?
答案1
得分: 0
你可以创建一个视图,接收专家ID和要传递的信息,在其中将信息保存到数据库。
步骤:
- “保存”链接的href属性将如下所示:
{% url 'your_view_name' expert.id %}
- 重写你的视图的
post
方法。 - 在
post
方法中,你将获取专家的id参数,获取专家信息,然后更新数据库。
在这些步骤中比较棘手的部分是如何根据ID获取专家数据?
你可以再次请求API并获取专家信息,然后根据需要保存它们,或者为了减少API调用并使它们在每个循环中只调用一次,你可以使用Redis等工具对响应进行缓存,或者将API的响应存储在数据库中。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论