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 に答える