python类shortcuts()的实例源码

compatibility.py 文件源码 项目:django-rest-framework-client 作者: qvantel 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def get_object_or_404(klass, *args, **kwargs):
    """
    Similar to django.shortcuts.get_object_or_404 but it also supports klass argument to be of type
    restframework.Model or PartiallyFiltered
    """
    if isinstance(klass, (ModelBase, PartiallyFiltered)):
        partially_filtered = klass if isinstance(klass, PartiallyFiltered) else klass.objects.all()
        model = partially_filtered.model
        try:
            return partially_filtered.get(*args, **kwargs)
        except model.DoesNotExist:
            raise Http404('No %s matches the given query.' % model._meta.object_name)
    else:
        return django_get_object_or_404(klass, *args, **kwargs)


问题


面经


文章

微信
公众号

扫码关注公众号