How to use transactions with Django REST framework?
<p>I wish to use Django REST framework to create a number of model objects "together" -- i.e. in a single transaction. </p>

<p>The objective is that each of the objects will only be visible at the (successful) end of the transaction.</p>

<p>How can I do that?</p>

<p>Use <code>atomic</code> from <code>django.db.transaction</code> as a decorator around a function performing the database operations you are after:</p>

<p>If <code>obj_list</code> contains a list of populated (but not saved) model objects, this will execute all operations as part of one transaction.</p>

<p><code>
@atomic
def save_multiple_objects(obj_list):
   for o in obj_list:
     o.save()
</code></p>

<p>If you want to save multiple objects as part of the same API request, then (for example), if they are all of the same type, then you could POST a list of objects to an API endpoint - see <a href="http://stackoverflow.com/questions/22881067/django-rest-framework-post-array-of-objects">Django REST framework post array of objects</a></p>

python django rest django-models django-rest-framework