You can use transaction.atomic:
https://docs.djangoproject.com/en/4.0/topics/db/transactions/#controlling-transactions-explicitly

On July 4, 2022 7:16:41 AM EDT, Sencer Hamarat <sencerhama...@gmail.com> wrote:
>Hi,
>
>Say I have a parent and a child model.
>
>And also there is a view which is using DRF serializers to save data into
>models.
>
>The saving operation steps are:
>
>First create parent parent object
>Then, create child object with parent object relation.
>
>The view code regarding the description above:
>
>parent_data = {
>    "foo": "bar",
>    "baz": "bar"
>}
>
>parent_serializer = ParentSerializer(data=parent_data)
>if parent_serializer.is_valid():
>    parent_serializer.save()
>
>    child_data = {
>        "parent_id": parent_serializer.data['id'],
>        "baz": "foo":
>        "bar": "baz"
>    }
>
>    child_serializer = ChildSerializer(data=child_data)
>    if child_serializer.is_valid():
>        child_serializer.save()
>
>
>if any exception is thrown while child_serializer saving, how to roll back
>the parent object, too?
>
>Is there any chance to make this happen in a transaction to roll back any
>related record?
>
>
>Kind regards,
>Sencer HAMARAT
>
>-- 
>You received this message because you are subscribed to the Google Groups 
>"Django users" group.
>To unsubscribe from this group and stop receiving emails from it, send an 
>email to django-users+unsubscr...@googlegroups.com.
>To view this discussion on the web visit 
>https://groups.google.com/d/msgid/django-users/CACp8TZh09RSgF2FzjcXNodbcykXYuE566UH%2B8zRtgDm_VfdtaA%40mail.gmail.com.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/BEE4B516-7426-4A07-B91B-9733537D4F92%40fattuba.com.

Reply via email to