M
M
matveyvarg2016-05-24 18:15:57
Django
matveyvarg, 2016-05-24 18:15:57

How to add custom field to Django Rest Framework errors?

It is necessary that if there are errors after validation, then the "status=error" field is added.
What method should be overwritten for this?
I use generic Views. (if this information is needed)
Ultimately, you should get something like this:

{   
    "status:"error",
    "data":[
        "source": [
            "This field may not be blank."
        ],
        "description": [
            "This field may not be blank."
        ]
    ]
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
marazmiki, 2016-05-24
@matveyvarg

You can do this by overriding the errors property of the serializer class:

# serializers.py

class MySerializer(serializers.ModelSerializer):
    # ...

    @property
    def errors(self):
        origin = super().errors
        origin['status'] = 'error'
        return origin

The correct answer is: you don't need to want it at all. HTTP code 400 speaks much more eloquently about the error

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question