serializers.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:django-rest-witchcraft 作者: shosca 项目源码 文件源码
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))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号