How can I modify the serializer.is_valid() method for existing rows on the database so it passes the validation

29 views Asked by At

I am trying to insert a list of objects by using serializer.save() but when I try to insert existing data it throws error and doesn't pass the validation how can I modify the is_valid() method or is there any way of skipping the existing objects and inserting the others inside the method

Saving:

data = sanitizer.to_dict()
serializer = serializer_class(data=data, many=True)
if serializer.is_valid(raise_exception=True):
   serializer.save()

Serializer:

from apps.trades.models import Trade
from rest_framework import serializers


class TradeSerializer(serializers.ModelSerializer):
    class Meta:
        model = Trade
        fields = ['identifier', 'issue_date', 'maturity_date', 
                  'invested_amount', 'interest_rate']

The validation error:

[{"identifier":["trade with this identifier already exists."]}

0

There are 0 answers