栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

什么是断言?应用场景?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

什么是断言?应用场景?

assert 是的作用?断言条件成立(布尔值为True)则继续往下,否则跑出异常,一般用于:满足某个条件之后,才能执行,否则应该跑出异常。写API的时候,继承GenericAPIViewclass GenericAPIView(views.APIView):         """         base class for all other generic views.         """         # You'll need to either set these attributes,         # or override `get_queryset()`/`get_serializer_class()`.         # If you are overriding a view method, it is important that you call         # `get_queryset()` instead of accessing the `queryset` property directly,         # as `queryset` will get evaluated only once, and those results are cached         # for all subsequent requests.         queryset = None         serializer_class = None         # If you want to use object lookups other than pk, set 'lookup_field'.         # For more complex lookup requirements override `get_object()`.         lookup_field = 'pk'         lookup_url_kwarg = None         # The filter backend classes to use for queryset filtering         filter_backends = api_settings.DEFAULT_FILTER_BACKENDS         # The style to use for queryset pagination.         pagination_class = api_settings.DEFAULT_PAGINATION_CLASS         def get_queryset(self):  assert self.queryset is not None, (      "'%s' should either include a `queryset` attribute, "      "or override the `get_queryset()` method."      % self.__class__.__name__  )  queryset = self.queryset  if isinstance(queryset, QuerySet):      # Ensure queryset is re-evaluated on each request.      queryset = queryset.all(  return queryset

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/362969.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号