I m using django rest framework to provide api to multiple application. single models being shared with these applications. And every applications are using some fields that no other apps are using. I have to write update method on serializer and take action as per the fields value received from these apps. when i write the update method and in case django doesn't finds any key it throws error. let see the code: Model:
class TaskChecker(models.Model):
taskName=models.CharField(max_length=50)
notified = models.BooleanField (default=False)
isDeleteRequest = models.BooleanField (default=False)
isDeactivateMe = models.BooleanField (default=False)
isActive = models.BooleanField (default=False)
class TaskSerializer(serializers.ModelSerializer):
class Meta:
model=TaskChecker
fields='__all__'
def update(self, instance, validated_data):
isDeleteRequest = validated_data['isDeleteRequest']
do some task
isActive= validated_data['isActive']
do some task
now the scenario is at a time either i will get validated_data['isDeleteRequest'] or validated_data['isActive']. in that case i get key error. how to resolve the issue? if i dont get the key at that time i should not get error. please help. thank you so much....
In TaskSerializer use the dict get method instead.
class TaskSerializer(serializers.ModelSerializer):
class Meta:
model=TaskChecker
fields='__all__'
def update(self, instance, validated_data):
isDeleteRequest = validated_data.get('isDeleteRequest', False)
do some task
isActive= validated_data.get('isActive', False)
do some task