Pass kwargs to model save method
I am stuck in weird situation i have this kwarg['device_id'] which i need to access in my save method , it there any way i can achieve this, the reason i want to avoid overriding update method because i have kept my external API calls in model save method , help is needed thanks
def save(self, *args, **kwargs):
device_uid = (getattr(self,'device_id')).upper()
device_name = getattr(self,'device_name')
device_profile_name = getattr(self,'device_profile_name')
app_id = getattr(self,'device_profile_name')
# check whether model is getting saved or updated
if self._state.adding:
device_status, error_remarks = add_device(app_id,device_profile_name,device_uid,device_name)
else:
print(device_name,device_uid,device_profile_name,app_id)
device_status, error_remarks = update_device(app_id,device_profile_name,device_uid,device_name) <<-- API call
if device_status == 200 :
super(DeviceSubscription, self).save(*args, **kwargs)
schedulars.check_device()
else:
print(error_remarks)
raise RequestException('err:{}'.format(er
#update device
@api_view(['PUT'])
def update_device(request,*args,**kwargs):
if kwargs['device_id']:
try:
device_ins = DeviceSubscription.objects.get(device_id = kwargs['device_id'])
serialized_data = DeviceAdditionSerializer(device_ins,request.data,partial = True)
if serialized_data.is_valid(raise_exception=True):
serialized_data.save()
return Response({'data':serialized_data.data},status=200)
except Exception as e:
return Response({'err':str(e)},status=400)
else:
return Response({'status':'bad request'},status=400)