django rest框架创建嵌套对象“模型”通过邮寄 [英] django rest framework create nested objects "Models" by POST

查看:148
本文介绍了django rest框架创建嵌套对象“模型”通过邮寄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试POST一个新的嵌套对象,问题只是创建顶对象(播放列表),但不要创建ChannelItem...



我的模型:

  class Playlist(models.Model):
provider = models.IntegerField )
channel_id = models.CharField(max_length = 100)
channel_version = models.CharField(blank = True,max_length = 100)
start = models.DateTimeField()
url = models.CharField(max_length = 500)


class ChannelItem(models.Model):
playlist = models.ForeignKey(播放列表,可编辑= False,related_name ='channelitems')
content_id = models.CharField(max_length = 100)
content_version = models.CharField(blank = True,max_length = 100)

我的序列化器:

  class ChannelItemSerializer(serializers.ModelSerializer):
类Meta:
model = ChannelItem
fields =('content_id','content_ver sion')
exclude =('id')
depth = 1


class PlaylistSerializer(serializers.ModelSerializer):

class Meta:
model = Playlist
fields =('id','provider','channel_id','channel_version','start',
'url','channelitems')
depth = 2

channelitems = ChannelItemSerializer()

我使用卷曲发布以下数据:

 '{provider:125,channel_id:xyz,channel_version :xsqt,
start:2012-12-17T11:04:35,url:http://192.168.1.83:8080/maaaaa,
channelitems :[{content_id:0.flv,content_version:ss},
{content_id:1.flv,content_version:ss}] // localhost:8000 / playlist_scheduler / playlists /

我收到消息:

  HTTP / 1.1 201 CREATED 
内容类型:application / json
Tran sfer-Encoding:chunked
日期:2012年12月17日,星期一20:12:54 GMT
服务器:0.0.0.0

{id:25,provider 125,channel_id:xyz,channel_version:xsqt,
start:2012-12-17T11:04:35,url:http:// localhost:8080 / something,
channelitems:[]}


解决方案

嵌套表示目前不支持读写,而应该是只读的。



您应该使用pk或超链接关系查找使用平面表示。 p>

如果您需要嵌套表示,您可能需要考虑使用两个单独的端点 - 平面可写端点和嵌套只读端点。


I'm trying POST a new a Nested Object, the problem is just create the "top" object (Playlist), but don't create the "ChannelItem"...

My Models:

class Playlist(models.Model):
    provider = models.IntegerField()
    channel_id = models.CharField(max_length=100)
    channel_version = models.CharField(blank=True, max_length=100)
    start = models.DateTimeField()
    url = models.CharField(max_length=500)


class ChannelItem(models.Model):
    playlist = models.ForeignKey(Playlist, editable=False, related_name='channelitems')
    content_id = models.CharField(max_length=100)
    content_version = models.CharField(blank=True, max_length=100)

My Serializer:

class ChannelItemSerializer(serializers.ModelSerializer):
    class Meta:
        model = ChannelItem
        fields = ('content_id', 'content_version')
        exclude = ('id')
        depth = 1


class PlaylistSerializer(serializers.ModelSerializer):

    class Meta:
        model = Playlist
        fields = ('id', 'provider', 'channel_id', 'channel_version', 'start', 
                  'url', 'channelitems')
        depth = 2

channelitems = ChannelItemSerializer()

I use the curl to post the following data :

'{"provider":125,"channel_id":"xyz", "channel_version":"xsqt", 
"start":"2012-12-17T11:04:35","url":"http://192.168.1.83:8080/maaaaa",
"channelitems":[{"content_id":"0.flv", "content_version":"ss"},
{"content_id":"1.flv","content_version":"ss"}]}' http://localhost:8000/playlist_scheduler/playlists/

I receive the message:

HTTP/1.1 201 CREATED
Content-Type: application/json
Transfer-Encoding: chunked
Date: Mon, 17 Dec 2012 20:12:54 GMT
Server: 0.0.0.0

{"id": 25, "provider": 125, "channel_id": "xyz", "channel_version": "xsqt",
"start":"2012-12-17T11:04:35", "url": "http://localhost:8080/something",
"channelitems": []}

解决方案

Nested representations do not currently support read-write, and should instead be read-only.

You should probably look into using a flat representation instead, using pk or hyperlinked relations.

If you need the nested representation, you may want to consider having two separate endpoints - a flat writable endpoint, and a nested read-only endpoint.

这篇关于django rest框架创建嵌套对象“模型”通过邮寄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆