Re: How to roll back related objects if any error happened.

2022-07-05 Thread Antonis Christofides
I'm not really an expert, but it strikes me that this doesn't work. If you are using MySQL or SQLite, make sure you read the Database-specific notes on Database transactions in the Django documentation. If y

Re: How to roll back related objects if any error happened.

2022-07-05 Thread Sencer Hamarat
Hi Antonis, The code that I tried is something like below: with transaction.atomic(): parent_data = { "foo": "bar", "baz": "bar" } parent_serializer = ParentSerializer(data=parent_data) if parent_serializer.is_valid(): parent_obj = parent_serializer.save()

Re: How to roll back related objects if any error happened.

2022-07-05 Thread Antonis Christofides
Could you show the code with your attempt to use atomic? On 05/07/2022 14.45, Sencer Hamarat wrote: Hi, I tried that but, when child_serializer.save() throws an exception, the row created by parent_serializer.save() at the database is not rolled back. Kind regards, Sencer HAMARAT On Mon,

Re: How to roll back related objects if any error happened.

2022-07-05 Thread Sencer Hamarat
Hi, I tried that but, when child_serializer.save() throws an exception, the row created by parent_serializer.save() at the database is not rolled back. Kind regards, Sencer HAMARAT On Mon, Jul 4, 2022 at 7:59 PM Ryan Nowakowski wrote: > You can use transaction.atomic: > > https://docs.django

Re: How to roll back related objects if any error happened.

2022-07-04 Thread Ryan Nowakowski
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 wrote: >Hi, > >Say I have a parent and a child model. > >And also there is a view which is using DRF serializers to save

How to roll back related objects if any error happened.

2022-07-04 Thread Sencer Hamarat
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: