我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用django.shortcuts._get_queryset()。
def get_object_or_403(klass, *args, **kwargs): """ Uses get() to return an object, or raises a PermissionDenied exception if the object does not exist. klass may be a Model, Manager, or QuerySet object. All other passed arguments and keyword arguments are used in the get() query. Note: Like with get(), an MultipleObjectsReturned will be raised if more than one object is found. """ queryset = _get_queryset(klass) try: return queryset.get(*args, **kwargs) except queryset.model.DoesNotExist: raise PermissionDenied
def get_object_or_none(klass, *args, **kwargs): queryset = _get_queryset(klass) try: return queryset.get(*args, **kwargs) except AttributeError: klass__name = klass.__name__ if isinstance(klass, type) else klass.__class__.__name__ raise ValueError( "First argument to get_object_or_none() must be a Model, Manager, " "or QuerySet, not '%s'." % klass__name ) except queryset.model.DoesNotExist: return None
def get_queryset_size(klass, *args, **kwargs): queryset = _get_queryset(klass) try: obj_list = list(queryset.filter(*args, **kwargs)) except AttributeError: klass__name = klass.__name__ if isinstance(klass, type) else klass.__class__.__name__ raise ValueError( "First argument get_queryset_size() must be a Model, Manager, or " "QuerySet, not '%s'." % klass__name ) if not obj_list: return 0 return len(obj_list)
def __init__(self, model_or_qs): self.queryset = _get_queryset(model_or_qs) self.model = self.queryset.model