"""Django Rest Framework view mixins."""fromdjango.core.exceptionsimportValidationErrorfromdjango.httpimportHttp404fromrest_frameworkimportstatusfromrest_framework.mixinsimportCreateModelMixinfromrest_framework.responseimportResponseclassPutAsCreateMixin(CreateModelMixin):""" Backwards compatibility with Django Rest Framework v2, which allowed creation of a new resource using PUT. """defupdate(self,request,*args,**kwargs):""" Create/update course modes for a course. """# First, try to update the existing instancetry:try:returnsuper(PutAsCreateMixin,self).update(request,*args,**kwargs)exceptHttp404:# If no instance exists yet, create it.# This is backwards-compatible with the behavior of DRF v2.returnsuper(PutAsCreateMixin,self).create(request,*args,**kwargs)# Backwards compatibility with DRF v2 behavior, which would catch model-level# validation errors and return a 400exceptValidationErroraserr:returnResponse(err.messages,status=status.HTTP_400_BAD_REQUEST)