I'm following the tutorial here. I have come across this:
def update(self, instance, validated_data):
instance.title = validated_data.get('title', instance.title)
instance.code = validated_data.get('code', instance.code)
instance.linenos = validated_data.get('linenos', instance.linenos)
instance.language = validated_data.get('language', instance.language)
instance.style = validated_data.get('style', instance.style)
instance.save()
return instance
This is probably a very simple question, but what is get
function here? I'm having trouble finding any documentation about what it is. I understand that there is a get
query function, but are these the same functions?
validated_data
is anOrderedDict
andOrderedDict.get(key, default)
is the method that fetches thevalue
for the given key, returning thedefault
if the key is missing from the dict.In other words:
instance.title = validated_data.get('title', instance.title)
will try to fetchtitle
from validated_data but will return the currentinstance.title
if thetitle
key is not present in the validated data.https://docs.python.org/2/library/collections.html#collections.OrderedDict https://docs.python.org/2/library/stdtypes.html#dict.get