2

APIを作成しました。リソースを取得すると、次のようになります。

{
    category: {
        id: "517ed1bff929f90e1152ad43",
        name: "Test category"
    },
    cost: "Free",
    description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras tincidunt augue in mi scelerisque quis condimentum velit sollicitudin. Nam eleifend posuere semper. Pellentesque non nulla et arcu ornare lacinia. Donec quis dui at velit placerat volutpat vitae sit amet lorem. ",
    genders: [
        "M",
        "F"
    ],
    id: "517ed1bff929f90e1152ad44",
    tags: [
        {
            id: "517ed1bff929f90e1152ad3f",
            name: "testing",
            resource_uri: "/api/v1/interests/517ed1bff929f90e1152ad3f/"
        },
        {
            id: "517ed1bff929f90e1152ad41",
            name: "coding",
            resource_uri: "/api/v1/interests/517ed1bff929f90e1152ad41/"
        }
    ],
    location: {
        lat_lng: [
            51.500705,
            -0.124575
        ],
        locality: "London",
        name: "Big Ben"
    },
    resource_uri: "/api/v1/events/517ed1bff929f90e1152ad44/",
    slug: "test-event1",
    title: "Test event1"
}

オブジェクトの送信を要求するのではなく、['testing', 'coding', 'python'] などのタグをリストとして送信できるようにしたい - これは、タグ オブジェクトが存在しない場合は作成するためです。

Tastypie でこれを行うにはどうすればよいですか?REST の原則に違反していますか?

4

1 に答える 1

0

GET と POST の両方で URI の代わりに名前を使用することを除いて、同様のことを行いました。これは、より内部的に一貫している可能性があります。

POST/PUT で名前を使用するだけの場合は、 resource の hydrate_tags メソッドを定義できるはずです。クライアントが URL を使用することを禁止し、代わりに名前の使用のみを許可する場合、次のようなことがうまくいくかもしれません。

def hydrate_tags(self, bundle):
    new_list = []
    for item in bundle:
        new_list.append(TagResource.build_bundle(data={name: item.data}))
    return new_list

上記は検証されていないことに注意してください - hydrate_tags がリストまたはバンドルを返す必要があるかどうかを確認していません。

于 2013-06-10T22:19:11.723 に答える