简体   繁体   English

在一个TastyPie API请求中POST多个对象

[英]POST multiple objects in one TastyPie API request

I'd like to create multiple related objects in a single post request if it's possible. 如果可能的话,我想在一个帖子请求中创建多个相关对象。 I have a app that has multiple games, and I want to post to the database the app activities for each game. 我有一个有多个游戏的应用程序,我想在数据库中发布每个游戏的应用程序活动。

Each activity object in the model has a game object as a foreign key, so I need to create the game, before I can create the activity objects. 模型中的每个活动对象都有一个游戏对象作为外键,因此我需要在创建活动对象之前创建游戏。

{
     "game": {
         "name":"monte",
         "app":"/api/v1/app/1/"
      },

     "activity":{
         "type":"eggs",
         "score":"0.90",
         "game":"_INSERT_MONTE_RESOURCE_URI_HERE_"
      },

     "activity":{
         "type":"spam",
         "score":"1.00",
         "game":"_INSERT_MONTE_RESOURCE_URI_HERE_"
      }
}

Is there a simple way to do this, or do I need to make 3 post requests from my app? 有一种简单的方法可以做到这一点,还是我需要从我的应用程序发出3个帖子请求? One to create the game, and then one for each of the activities? 一个是创建游戏,然后是每个活动一个?

I thought maybe a PATCH would work, but then I realized that I wouldn't know the game resource URI to assign to each of the activities when I sent my patch request. 我想也许PATCH可以工作,但后来我意识到当我发送补丁请求时,我不知道要分配给每个活动的游戏资源URI。 I suppose I could create the game in one request, and then the activities in a patch request, I'm simply hoping that it's possible to do it all in one batch. 我想我可以在一个请求中创建游戏,然后在补丁请求中创建活动,我只是希望可以在一个批处理中完成所有操作。

use related name for the fields which accepts the creation of related objects 使用相关名称作为接受相关对象创建的字段

http://django-tastypie.readthedocs.org/en/v0.10.0/fields.html#tastypie.fields.RelatedField.related_name http://django-tastypie.readthedocs.org/en/v0.10.0/fields.html#tastypie.fields.RelatedField.related_name

RelatedField.related_name RelatedField.related_name

Used to help automatically populate reverse relations when creating data. 用于帮助在创建数据时自动填充反向关系。 Defaults to None. 默认为无。

In order for this option to work correctly, there must be a field on the other Resource with this as an attribute/instance_name. 为了使此选项正常工作,其他资源上必须有一个字段,并将其作为属性/ instance_name。 Usually this just means adding a reflecting ToOneField pointing back. 通常这只是意味着添加一个反射的ToOneField指向后面。

Example: 例:

class EntryResource(ModelResource): authors = fields.ToManyField('path.to.api.resources.AuthorResource', 'author_set', related_name='entry')

    class Meta:
        queryset = Entry.objects.all()
        resource_name = 'entry'

class AuthorResource(ModelResource):
    entry = fields.ToOneField(EntryResource, 'entry')

class Meta:
    queryset = Author.objects.all()
    resource_name = 'author'

Use of related_name do the task. 使用related_name可以完成任务。 it maps the objects of related fields and automatically populates the relations when creating data. 它映射相关字段的对象,并在创建数据时自动填充关系。

See the documentation section entitled Bulk Operations . 请参阅标题为批量操作的文档部分。 It begins: 它始于:

As an optimization, it is possible to do many creations, updates, and deletions to a collection in a single request by sending a PATCH to the list endpoint. 作为优化,可以通过将PATCH发送到列表端点,在单个请求中对集合进行许多创建,更新和删除。

And here's their example: 这是他们的榜样:

curl --dump-header - -H "Content-Type: application/json" -X PATCH --data '{"objects": [{"body": "Surprise! Another post!.", "pub_date": "2012-02-16T00:46:38", "slug": "yet-another-post", "title": "Yet Another Post"}], "deleted_objects": ["http://localhost:8000/api/v1/entry/4/"]}'  http://localhost:8000/api/v1/entry/

You can create resource for activity and use fields.ToManyField: https://django-tastypie.readthedocs.org/en/latest/resources.html#reverse-relationships 您可以为活动和使用字段创建资源.ToManyField: https//django-tastypie.readthedocs.org/en/latest/resources.html#reverse-relationships

This will add urls to activity resources. 这会将URL添加到活动资源中。 To fully inline data for activities just pass (full=True, full_list=True) as arguments to ToManyField: https://django-tastypie.readthedocs.org/en/latest/fields.html#tastypie.fields.RelatedField.full_list 要完全内联活动数据,只需传递(full = True,full_list = True)作为ToManyField的参数: https ://django-tastypie.readthedocs.org/en/latest/fields.html#tastypie.fields.RelatedField.full_list

If the game resource look like: 如果游戏资源如下:

class GameResource(ModelResource):
    activities = fields.ToManyField(ActivityResource, 'activities', full=True)

Following the note in tastypie documentation: 按照tastypie文档中的说明:

Tastypie encourages “round-trippable” data, which means the data you can GET should be able to be POST/PUT'd back to recreate the same object. Tastypie鼓励“round-trippable”数据,这意味着您可以获取的数据应该能够POST / PUT返回以重新创建相同的对象。 If you're ever in question about what you should send, do a GET on another object & see what Tastypie thinks it should look like. 如果您对要发送的内容有疑问,请在另一个对象上进行GET,看看Tastypie认为应该是什么样子。

You will be able to create all in one batch. 您将能够在一个批次中创建所有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM