0

初めてdjango-rest-framework APIを使用しています。これが私の質問です:

2 つのテーブルがあるデータベースを設計する必要があります。

  1. サーバー => IP アドレスやサーバー名などのサーバー情報を保存するには

id: INT, name: VARCHAR, ip_address: VARCHAR

  1. デプロイ => デプロイ日とコメント メッセージを含むサーバーへのデプロイ

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 ドキュメントについて読みましたが、上記の機能を実装する方法がわかりませんでした。

これを実装する方法についてアイデアがある場合、または別のデータベース設計がこの要件により適していると思われる場合はお知らせください。

ありがとう!

4

1 に答える 1

0

私のものと非常によく似たこの質問を見つけました。 Django RESTフレームワークでネストされたリソースにフィルタを適用するにはどうすればよいですか?

そして、私はSerializerMethodField

于 2013-09-09T20:43:50.013 に答える