2👍
✅
Finally I found my solution by changing the method update
to partial_update
. Apparently update method updates all the field while in above case I am attempting the field called response_text
in the Review model which are setting other fields to null if they could be. Also after doing that I had to change the request from PUT
to PATCH
in frontend. Also I had to do some other minor coding changes like removing supervisor
and supervisee
fields from read_only_fields
from ResponseSerializer
. Updated code for ResponseViewset
is shown below:
class ResponseViewSet(viewsets.ModelViewSet):
queryset = Review.objects.all()
permission_classes = [
permissions.IsAuthenticated,
#permissions.AllowAny,
]
serializer_class = ResponseSerializer
def partial_update(self, request,*args, **kwargs):
obj = self.get_object()
serializer = self.serializer_class(obj, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
mail_text = "Hi {},\n\nYou got a response for your 1:1 from {}.\n\nClick below to see the response:\n\n{}".format(
serializer.data["supervisor_name"],
serializer.data["supervisee_name"],
get_product_link("UMS/reviewsForDR"),
)
try:
if not settings.DEFAULT_EMAIL_RECIPIENTS:
settings.DEFAULT_EMAIL_RECIPIENTS.append(
# supervisor_email
serializer.data["supervisor_email"]
)
send_mail(
subject="New Response Received",
message=mail_text,
from_email=settings.DEFAULT_FROM_EMAIL,
recipient_list=settings.DEFAULT_EMAIL_RECIPIENTS,
fail_silently=False,
)
settings.DEFAULT_EMAIL_RECIPIENTS = []
except (SMTPRecipientsRefused, SMTPSenderRefused):
LOGGER.exception("There was a problem submitting the form.")
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Source:stackexchange.com