zoukankan      html  css  js  c++  java
  • Xadmin自定义detail小感叹号显示在列上面

    1.修改源码

    # 路径xadminpluginsdetails.py
    # 大概在18行左右
        def result_item(self, item, obj, field_name, row):
    
            if (self.show_all_rel_details or (field_name in self.show_detail_fields)):
                rel_obj = None
                if hasattr(item.field, 'remote_field') and isinstance(item.field.remote_field, models.ManyToOneRel):
                    rel_obj = getattr(obj, field_name)
                # =====================添加这一段===========================================
                elif hasattr(self.admin_view, 'conf_show_detail_fields') and field_name in self.admin_view.conf_show_detail_fields:
                    rel_obj = getattr(obj, self.admin_view.conf_show_detail_fields[field_name])
                # =====================添加结束===========================================
                elif field_name in self.show_detail_fields:
                    rel_obj = obj
    
                if rel_obj:
                    if rel_obj.__class__ in site._registry:
                        try:
                            model_admin = site._registry[rel_obj.__class__]
                            has_view_perm = model_admin(self.admin_view.request).has_view_permission(rel_obj)
                            has_change_perm = model_admin(self.admin_view.request).has_change_permission(rel_obj)
                        except:
                            has_view_perm = self.admin_view.has_model_perm(rel_obj.__class__, 'view')
                            has_change_perm = self.has_model_perm(rel_obj.__class__, 'change')
                    else:
                        has_view_perm = self.admin_view.has_model_perm(rel_obj.__class__, 'view')
                        has_change_perm = self.has_model_perm(rel_obj.__class__, 'change')
    
                if rel_obj and has_view_perm:
                    opts = rel_obj._meta
                    try:
                        item_res_uri = reverse(
                            '%s:%s_%s_detail' % (self.admin_site.app_name,
                                                 opts.app_label, opts.model_name),
                            args=(getattr(rel_obj, opts.pk.attname),))
                        if item_res_uri:
                            if has_change_perm:
                                edit_url = reverse(
                                    '%s:%s_%s_change' % (self.admin_site.app_name, opts.app_label, opts.model_name),
                                    args=(getattr(rel_obj, opts.pk.attname),))
                            else:
                                edit_url = ''
                            item.btns.append(
                                '<a data-res-uri="%s" data-edit-uri="%s" class="details-handler" rel="tooltip" title="%s"><i class="fa fa-info-circle"></i></a>'
                                % (item_res_uri, edit_url, _(u'Details of %s') % str(rel_obj)))
                    except NoReverseMatch:
                        pass
    
            return item

    2.在注册类中添加配置

    # 配置当前字段显示哪个关联字段 
    # partner_sku 要显示感叹号的列
    # inside_sku 要显示详情的列
    conf_show_detail_fields = {'partner_sku': 'inside_sku'}
  • 相关阅读:
    取消a标签的页面跳转
    js 获取元素内部文本
    彻底取消Myeclipse对js文件的校验
    控制流程之if判断
    格式化的三种输出方式和基本运算符以及他们的优先级
    解压缩和python如何与用户交互
    数据类型基础和各种数据类型
    花式赋值和注释
    变量 及其命名和使用规范
    进入的python世界。了解执行python的交互方式和常用开发工具
  • 原文地址:https://www.cnblogs.com/wtil/p/12627877.html
Copyright © 2011-2022 走看看