1

I'm trying to use Django Tastypie to allow an implementor to POST/PUT data to create models that are related several levels deep. So POST something like this (as JSON) to create let's call it a Blob, which has many Foos, each of which has many Bars (both models as well):

{"foos":[
    {"bars":[
        {"baz":"boo"}
    ]}
]}

The model relations are all set in my models.py and resources.py scripts so that Blobs have many Foos and Foos have many Bars. The problem is that in doing this, Tastypie seems to only go a single level deep with obj_create doing the related fields, so the Bars never get created, and it kicks back an error if that field is required or nothing gets stored if it isn't. How do I go about getting Tastypie to traverse down through the levels?

As a side note, I know I could use the separate Bar endpoint to create those first, and then pass in the resource URIs with the given Foo. But for arguments sake let's say that's not possible, I need to take the entire data representing a Blob as a single POST.

4

1 回答 1

0

我猜 Tastypie 不支持开箱即用的这种关系。这可能是因为您通常不需要如此深的嵌套。

您可以将它们放在模型中,但是如果您想以这种方式公开它,那么拥有一个平坦的资源怎么样。鉴于您可以使用相关模型的属性,例如some_field = fields.CharField( attribute = 'relatedmodel__field' )我认为会被保存的那些。

这样你的 API 会更好,你仍然可以得到你需要的东西。

于 2012-09-08T10:50:14.127 回答