Note that there are some explanatory texts on larger screens.

plurals
  1. POTastypie - Nested resources are updated instead of created on POST
    primarykey
    data
    text
    <p>I am trying to POST a resource that consists basically of a list of child resources. Here are my resources:</p> <pre><code>class MovementResource(ModelResource): transactions = fields.ToManyField('stock.api.TransactionResource', 'transaction_set', related_name='movement', full=True) class Meta: queryset = Movement.objects.all() resource_name = 'movement' always_return_data = True authorization = Authorization() class TransactionResource(ModelResource): movement = fields.ToOneField(MovementResource, 'movement') product = fields.ToOneField(ProductResource, 'product', full=True) class Meta: queryset = Transaction.objects.all() resource_name = 'transaction' authorization = Authorization() </code></pre> <p>This represents a movement of various products in an inventory. The movement form is just a list of inlines, one for each transaction. It doesn't use django forms at all.</p> <p>My problem is, when trying to POST a movement specifying the list of transactions to be created, I find that instead of inserting new transactions Tastypie is updating an existing one. This is the JSON for a POST request, representing a movement with only one transaction:</p> <pre><code>{ 'transactions': [ { 'product': '/api/v1/product/3/', 'quantity': '1' } ] } </code></pre> <p>The response JSON shows the movement has been created, but the transaction has an id that existed from before and had the same product and quantity. The movement FK in this transaction object has been updated to point to the newly created Movement. Response data:</p> <pre><code>{ 'date': '2013-02-07 ···', 'id': '66', 'resource_uri': '/api/v1/movement/66/', 'transactions': [ { 'date': '2013-01-30 ···', # Should be the same as parent movement's date 'id': '30', 'movement': '/api/v1/movement/66/', 'product': { ··· product resource data ··· }, 'quantity': '1', 'resource_uri': '/api/v1/transaction/30/', } ] } </code></pre> <p>Am I missing something? Shouldn't Tastypie create the nested resources being this a POST to a list endpoint? I've tried both using ForeignKey and ToOneField to the MovementResource, to see if it would make any difference, but to no success. (I actually couldn't find the difference between the two.)</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload