[Answered ]-How to deal with .errors for Django API framework

1๐Ÿ‘

โœ…

If deviceserializer.is_valid() fails, then the if condition will shortcircuit and thus not call devdserializer.is_valid(). You thus should ensure that it calls devdserializer.is_valid() as well:

if deviceserializer.is_valid() and devdserializer.is_valid():   
    device_instance = deviceserializer.save()
    devdserializer.save(DD2DKEY=device_instance)                
    results = {
        "device":deviceserializer.data,
        "device_details" : devdserializer.data,
    }
    return Response(results, status=status.HTTP_201_CREATED)
else:
    #       โ†“ ensure that is_valid() is called
    devdserializer.is_valid()
    errors = {
        "device":deviceserializer.errors,
        "device_details" : devdserializer.errors,
    }
    return Response(errors, status=status.HTTP_400_BAD_REQUEST)

Leave a comment