【问题标题】:Design history tracking database using django-rest-framework使用 django-rest-framework 设计历史跟踪数据库
【发布时间】:2013-09-09 18:30:16
【问题描述】:

我是第一次使用 django-rest-framework api。这是我的问题:

我需要设计一个有两个表的数据库:

  1. 服务器 => 保存服务器信息,例如 ip 地址和服务器名称

id: INT, name: VARCHAR, ip_address: VARCHAR

  1. Deploy => 服务器上的部署,包括部署日期和评论消息

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 文档,但不知道如何实现我上面列出的功能。

如果您对如何实现此功能有任何想法,或者您认为另一种数据库设计更适合此要求,请告诉我。

谢谢!

【问题讨论】:

    标签: django rest django-rest-framework


    【解决方案1】:

    发现这个问题与我的非常相似。 How can I apply a filter to a nested resource in Django REST framework?

    我使用SerializerMethodField解决了我的问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-02
      • 2010-09-13
      • 1970-01-01
      • 1970-01-01
      • 2013-03-17
      • 1970-01-01
      • 2011-10-03
      • 1970-01-01
      相关资源
      最近更新 更多