Python django.views.generic.detail 模块,SingleObjectMixin() 实例源码

我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用django.views.generic.detail.SingleObjectMixin()

项目:talkativot    作者:lablup    | 项目源码 | 文件源码
def get_json_data(self, context):
        '''
        Default implementation: remove some auto-injected but not
        JSON-serializable context variables.
        If self.context_json_filter is set to a collection such as list or
        set, then it instead preserves only the key-value pairs whose key is
        in the filter.

        You may override this method to implement your own serialization and
        context filtering process.
        '''
        if self.context_json_filter is None:
            if isinstance(self, SingleObjectMixin):
                name = self.get_context_object_name(self.object)
                if name in context: del context[name]
                if 'object' in context: del context['object']
            if isinstance(self, ContextMixin):
                if 'view' in context: del context['view']
            return context
        else:
            return {k: v for k, v in context.items()
                    if k in self.context_json_filter}
项目:quips    作者:infinitewarp    | 项目源码 | 文件源码
def get_object(self, queryset=None):
        """
        Override default behavior from django.views.generic.SingleObjectMixin
        to exclusively get the Quip object by its uuid.
        """
        if queryset is None:
            queryset = self.get_queryset()

        uuid = self.kwargs.get('uuid')
        try:
            queryset = queryset.filter(uuid=uuid)
            obj = queryset.get()
        except (ValidationError, ValueError, queryset.model.DoesNotExist):
            # ValidationError and ValueError are to deal with malformed inputs
            # that aren't valid uuids.
            raise Http404(_("No %(verbose_name)s found matching the query") %
                          {'verbose_name': queryset.model._meta.verbose_name})

        return obj