我第一次使用django-rest-framework api。这是我的问题:
我需要设计一个有两个表的数据库:
id: INT, name: VARCHAR, ip_address: VARCHAR
id: INT, server_id: FK to Server, deploy_date: DATETIME, message: VARCHAR
我被要求跟踪部署信息并设计以下API:
get /servers/ => get all the server information with the latest deploy on that server
Example:
[
{
"id" : 1,
"name" : "qa-001",
"ip_address" : "192.168.1.1",
"deploy" :
{
"id" : 1,
"deploy_date" : "2013-09-09 12:00:00",
"message" : "test new api"
}
},
{
"id" : 2,
"name" : "qa-002",
"ip_address" : "192.168.1.2",
"deploy" :
{
"id" : 2,
"deploy_date" : "2013-09-10 12:00:00",
"message" : "test second message"
}
}
]
get /deploys/ => get all the deploy information
Example:
[
{
"id" : 1,
"deploy_date" : "2013-09-09 12:00:00",
"message" : "test new api",
"server_id" : 1
},
{
"id" : 2,
"deploy_date" : "2013-09-10 12:00:00",
"message" : "test second message",
"server_id" : 2
},
{
"id" : 3,
"deploy_date" : "2013-09-08 12:00:00",
"message" : "test new api",
"server_id" : 1
}
]
// Deploy 3 does not show up in the get servers return json
// because deploy 1 was deployed on the same server but later than deploy 3.
POST /deploys/ => To insert a new deploy
POST /servers/ => To insert a new server
...
我玩过django-rest-framework教程代码并阅读了不同的api文档,但无法弄清楚如何实现我上面列出的功能。
如果您对如何实现这一点或者您认为其他数据库设计更符合此要求,请告诉我。
谢谢!
答案 0 :(得分:0)
发现这个与我非常相似的问题。 How can I apply a filter to a nested resource in Django REST framework?
我使用SerializerMethodField