I want to update only specific fields in my model. This is my models.py
class CheckinQnResult(models.Model):
client = models.ForeignKey(User, on_delete=models.CASCADE,null=True, blank=True,related_name='client_create')
appt = models.ForeignKey(Appointment, null=True, blank=True, on_delete=models.SET_NULL, related_name='appt_qn')
is_first_visit = models.BooleanField(default=False)
document = models.FileField(upload_to='documents', null=True, blank=True)
duration_of_sickness = models.CharField(max_length=100, null=True, blank=True)
age_noticed = models.CharField(max_length=100, null=True, blank=True)
first_sought_age = models.CharField(max_length=100, null=True, blank=True)
med_treated = models.CharField(max_length=1000, null=True, blank=True)
I want to update only specific fields in my model. This is my view.py
class DocumentView(ViewSet):
model = CheckinQnResult
serializer_class = DocumentAlldataSerializer
permission_classes = [IsAuthenticated]
authentication_classes = [BasicAuthentication, TokenAuthentication, JSONWebTokenAuthentication]
def create(self,request):
serializer = self.serializer_class(data=request.data)
if serializer.is_valid():
ch_qn = serializer.save()
appt = Appointment.objects.get(id=request.data.get('appt_id'))
appt.save()
ch_qn.appt = appt
ch_qn.save()
return Response({'status': 'success', 'message': 'Check in questions submitted successfully'},
status=status.HTTP_200_OK)
else:
return Response(serializer.errors)
def getdocument(self,request):
queryset = CheckinQnResult.objects.filter(client=request.user)
serializer = DocumentAlldataSerializer(queryset,many=True)
return Response({'status':'success','data':serializer.data},status=status.HTTP_200_OK)
)
def update(self, request, *args, **kwargs):
partial = kwargs.pop('partial', False)
instance = CheckinQnResult.objects.get(id=request.data['id'])
serializer = self.serializer_class(instance, data=request.data, partial=partial)
serializer.is_valid(raise_exception=True)
self.perform_update(serializer)
return Response({'status': 'success', 'data': serializer.data}, status=status.HTTP_200_OK)
def delete(self, instance):
instance = CheckinQnResult.objects.get(id=self.request.GET.get('id'))
instance.delete()
return Response({'status': 'success', 'message': 'Document Deleted successfully'}, status=status.HTTP_200_OK)
I want to update only specific fields in my model. This is my serializer.py
class DocumentAlldataSerializer(serializers.ModelSerializer):
appt = AppointmentSerializer()
client=UserSerializer()
class Meta:
model = CheckinQnResult
fields = '__all__'
extra_fields = ['appt','client']
Here I only want to update the document without affecting other field. When I add other field also, it works but I do not want to add other.