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, Jul 4, 2022 at 7:59 PM Ryan Nowakowski <r...@fattuba.com> wrote:

    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/BEE4B516-7426-4A07-B91B-9733537D4F92%40fattuba.com
    
<https://groups.google.com/d/msgid/django-users/BEE4B516-7426-4A07-B91B-9733537D4F92%40fattuba.com?utm_medium=email&utm_source=footer>.

--
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/CACp8TZiN2bWGc6FKERxRm83_bbyaRZkhr%3DoG_scATQoxREzPJg%40mail.gmail.com <https://groups.google.com/d/msgid/django-users/CACp8TZiN2bWGc6FKERxRm83_bbyaRZkhr%3DoG_scATQoxREzPJg%40mail.gmail.com?utm_medium=email&utm_source=footer>.

--
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/aa91c012-95d7-ff3d-bfdc-0bcfcb32a3ee%40antonischristofides.com.

Reply via email to