我们从Python开源项目中,提取了以下28个代码示例,用于说明如何使用django.contrib.admin.options.TO_FIELD_VAR。
def add_preserved_filters(context, url, popup=False, to_field=None): opts = context.get('opts') preserved_filters = context.get('preserved_filters') parsed_url = list(urlparse(url)) parsed_qs = dict(parse_qsl(parsed_url[4])) merged_qs = dict() if opts and preserved_filters: preserved_filters = dict(parse_qsl(preserved_filters)) match_url = '/%s' % url.partition(get_script_prefix())[2] try: match = resolve(match_url) except Resolver404: pass else: current_url = '%s:%s' % (match.app_name, match.url_name) changelist_url = 'admin:%s_%s_changelist' % (opts.app_label, opts.model_name) if changelist_url == current_url and '_changelist_filters' in preserved_filters: preserved_filters = dict(parse_qsl(preserved_filters['_changelist_filters'])) merged_qs.update(preserved_filters) if popup: from django.contrib.admin.options import IS_POPUP_VAR merged_qs[IS_POPUP_VAR] = 1 if to_field: from django.contrib.admin.options import TO_FIELD_VAR merged_qs[TO_FIELD_VAR] = to_field merged_qs.update(parsed_qs) parsed_url[4] = urlencode(merged_qs) return urlunparse(parsed_url)
def change_view(self, request, object_id, form_url='', extra_context=None): """ Override this function to hide the sumbit row from the user who has view only permission """ to_field = request.POST.get( TO_FIELD_VAR, request.GET.get(TO_FIELD_VAR) ) model = self.model opts = model._meta # TODO: Overriding the change_view costs 1 query more (one from us # and another from the super) obj = self.get_object(request, unquote(object_id), to_field) if self.has_view_permission(request, obj) and \ not self._has_change_only_permission(request, obj): extra_context = extra_context or {} extra_context['title'] = _('View %s') % force_text( opts.verbose_name) extra_context['show_save'] = False extra_context['show_save_and_continue'] = False extra_context['show_save_and_add_another'] = False extra_context['show_save_as_new'] = False inlines = self.get_inline_instances(request, obj) for inline in inlines: if (inline._has_change_only_permission(request, obj) or inline.has_add_permission(request)): extra_context['show_save'] = True extra_context['show_save_and_continue'] = True break return super(AdminViewPermissionModelAdmin, self).change_view( request, object_id, form_url, extra_context)
def get_context_data(self, **kwargs): """ Gets context data required for the admin view :param kwargs: Default keyword args :type kwargs: {} :return: Dict of data for the admin template """ context_data = super(AdminView, self).get_context_data(**kwargs) opts = self.admin_site.model._meta context_data.update({ 'add': False, 'change': False, 'has_add_permission': self.admin_site.has_add_permission(self.request), 'has_change_permission': self.admin_site.has_change_permission(self.request), 'has_delete_permission': self.admin_site.has_delete_permission(self.request), 'has_absolute_url': False, 'absolute_url': None, 'opts': opts, 'content_type_id': get_content_type_for_model(self.admin_site.model).pk, 'save_as': self.admin_site.save_as, 'save_on_top': self.admin_site.save_on_top, 'to_field_var': TO_FIELD_VAR, 'is_popup_var': IS_POPUP_VAR, 'is_popup': False, 'app_label': opts.app_label, }) return context_data