Python 在单个POST请求中创建相关记录

Python 在单个POST请求中创建相关记录,python,django,tastypie,Python,Django,Tastypie,使用tastypie,我如何在一个POST请求中创建一个记录和几个相关记录 例如,我有以下两种资源: class SongResource(ModelResource): playlists = fields.ToManyField('playlists.api.resources.PlaylistResource', 'playlist_set', related_name = "song", full=True) class Meta: queryset =

使用tastypie,我如何在一个POST请求中创建一个记录和几个相关记录

例如,我有以下两种资源:

class SongResource(ModelResource):
    playlists = fields.ToManyField('playlists.api.resources.PlaylistResource', 'playlist_set', related_name = "song", full=True)

    class Meta:
        queryset = Song.objects.all();
        resource_name = 'song'
        authorization = Authorization()


class PlaylistResource(ModelResource):
    song = fields.ToOneField(SongResource, 'song', full=True)

    class Meta:
        queryset = Playlist.objects.all()
        resource_name = 'playlist'
        authorization = Authorization()
我想使用post请求和数据一次性创建一个新的播放列表及其歌曲,如下所示:

    var data = JSON.stringify({
        'name': 'My playlist.',
        'songs': [{'title': 'Song 1'}, {'title': 'Song 2'}, {'title': 'Song 3'}]
    });
class SomeResource(ModelResource):
    class Meta:
        # yadda yadda

    def obj_create(self, bundle, request, **kwargs):
        print "hey we're in object create"
        # do something with bundle.data, this will have your songs in it
        return super(SomeResource, self).obj_create(bundle, request, **kwargs)
curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"name":"playlist_name", "field2":"value2", "song": ["/api/v1/song/1/"]}' http://localhost:8000/api/v1/playlist/

那不行。我被告知,“给‘song’字段的数据不是URI,也不是字典,也没有‘pk’属性”。是否可以像这样一次插入一张唱片,或者我需要为播放列表和每首歌曲发送单独的请求?

只需非常、非常快速地使用它(我是tastypie新手,所以对此持保留态度)-我认为您可以通过在资源中重写obj_create()方法来实现这一点。大概是这样的:

    var data = JSON.stringify({
        'name': 'My playlist.',
        'songs': [{'title': 'Song 1'}, {'title': 'Song 2'}, {'title': 'Song 3'}]
    });
class SomeResource(ModelResource):
    class Meta:
        # yadda yadda

    def obj_create(self, bundle, request, **kwargs):
        print "hey we're in object create"
        # do something with bundle.data, this will have your songs in it
        return super(SomeResource, self).obj_create(bundle, request, **kwargs)
curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"name":"playlist_name", "field2":"value2", "song": ["/api/v1/song/1/"]}' http://localhost:8000/api/v1/playlist/

您希望执行以下操作:

    var data = JSON.stringify({
        'name': 'My playlist.',
        'songs': [{'title': 'Song 1'}, {'title': 'Song 2'}, {'title': 'Song 3'}]
    });
class SomeResource(ModelResource):
    class Meta:
        # yadda yadda

    def obj_create(self, bundle, request, **kwargs):
        print "hey we're in object create"
        # do something with bundle.data, this will have your songs in it
        return super(SomeResource, self).obj_create(bundle, request, **kwargs)
curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"name":"playlist_name", "field2":"value2", "song": ["/api/v1/song/1/"]}' http://localhost:8000/api/v1/playlist/

祝你好运

但这不是假设已经创作了一首歌吗?我需要同时创建播放列表和歌曲,我想这样做:curl--dump header--H“Content Type:application/json”-X POST--data'{“name”:“playlist_name”,“field2”:“value2”,“song”:[“title”:“song title”,“field2”:“field2”,…]),这或多或少是我在上面的json示例中所做的,除了我需要传递多首歌曲,而且你的JSON看起来只传递一首歌曲,对吗?哎呀,我在之前的评论中省略了字典括号。您可以像这样传递多首歌曲:curl--dump header--H“Content Type:application/json”-X POST--data'{“name”:“playlist_name”,“field2”:“value2”,“song”:[{“title”:“song title 1”,“field2”:“fing title 2”},{“title”:“song title 2”,“field2”:“field2”}}}localhost:8000/api/v1/playlay是的,据我所知,这就是我已经在做的事情,但它似乎不起作用。请参见上面的JSON示例。这不一样吗,还是我遗漏了什么?我希望它能自动完成,但这肯定能奏效。谢谢,我今天就试试。