def get_object(self, validated_data, instance=None):
"""
Returns model object instance using the primary key values in the `validated_data`.
If the instance is not found, depending on serializer's `allow_create` value, it will create a new model
instance or raise an error.
"""
pks = self.get_primary_keys(validated_data)
checked_instance = None
if pks:
checked_instance = self.session.query(self.model).get(pks)
else:
checked_instance = instance
if checked_instance is not None:
return checked_instance
if self.allow_create:
return self.model()
if self.allow_null:
return checked_instance
raise ValidationError('No instance of `{}` found with primary keys `{}`'.format(self.model.__name__, pks))
评论列表
文章目录