Skip to content

Update method doesn't raise a serializer error on save with invalid data. #304

@jalondono

Description

@jalondono

I am trying to created an object from a nested field, using a patch request. Basically there exist one to many relationship between them, and also a unique together constrain at the model level.

url: PATCH: http://127.0.0.1:8000/api/v2/product_batches/11791/

Payload:

{
    "ingredient_mixes":{
        "create":[{
            "ingredient": 23,
            "quantity": 3,
            "unit_of_measurement": "kg",
            "product_batch": 11791,
            "is_removed": false,
            "removed_at": "2021-11-18T21:58:39.512Z",
            "remote_id": null
        },
        {
            "ingredient": 17,
            "quantity": 100,
            "unit_of_measurement": "L",
            "product_batch": 11791,
            "is_removed": false,
            "removed_at": "2021-11-18T21:58:39.512Z",
            "remote_id": null
        }]
    }
}

When I try to create an object that break the unique together constrain, let's that send again the same payload. The update method, get and an error from the serializer.

Screen Shot 2022-04-01 at 10 42 41 AM

Because the data is not valid, It violates the unique together constrain as I expect, but the request return a 500 code with the following message: "AssertionError: You cannot call .save() on a serializer with invalid data" Instead of return serializers.ValidationError.
Is this behaviour what are you expecting?
I would like to handle that error, I think in override the update method and put one try and except in order to catch the ValidationError and return a custom message.
Do you have any suggestion to this?
Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions