• Xadmin-自定义字段支持实时编辑


    # 需要显示的列
    list_display = ['fbm_display']
    # 实时编辑功能
        list_editable = ['reason_one', 'low_price', 'artificial_confirm_stock_no_fbn'] 

    # 因为这个字段后期要加一些显示的逻辑,所以需要自定义,但是又希望能支持实时编辑

    def fbm_display(self, row):

      return row.artificial_confirm_stock_no_fbn

    fbm_display.short_description
    = '补FBM'

    1. 修改models

    class Goods(models.Model):
        '''
        商品表
        '''
       
        artificial_confirm_stock_no_fbn = models.IntegerField(verbose_name='人工海外补货', default=-1)
       
    # 在表中添加一个函数,函数名要跟自定义字段的名字相同
        def fbm_display(self):
            return 'artificial_confirm_stock_no_fbn'

    2 修改 xadmin/util.py

    def lookup_field(name, obj, model_admin=None):
        opts = obj._meta
    
        try:
            f = opts.get_field(name)
        except models.FieldDoesNotExist:
            # For non-field values, the value is either a method, property or
            # returned via a callable.
            if callable(name):
                attr = name
                value = attr(obj)
            elif (
                    model_admin is not None
                    and hasattr(model_admin, name)
                    and name not in ('__str__', '__unicode__')
            ):
                attr = getattr(model_admin, name)
                value = attr(obj)
            else:
                if is_rel_field(name, obj):
                    parts = name.split("__")
                    rel_name, sub_rel_name = parts[0], "__".join(parts[1:])
                    rel_obj = getattr(obj, rel_name)
                    if rel_obj is not None:
                        return lookup_field(sub_rel_name, rel_obj, model_admin)
                attr = getattr(obj, name)
                if callable(attr):
                    value = attr()
                else:
                    value = attr
            f = None
            # 目的是为了传回字段
            if hasattr(obj, name):
                field = getattr(obj, name)()
                opts = obj._meta
                f = opts.get_field(field)
            # 添加完毕
        else:
            attr = None
            value = getattr(obj, name)
        return f, attr, value

    3.修改 xadmin/plugins/editable.py

      def result_item(self, item, obj, field_name, row):
            # 将list_display中的自定义字段名 跟 list_editable中的字段名绑定
            model_fields = [f.name for f in self.opts.fields]
            if hasattr(self.model, field_name) and field_name not in model_fields:
                field_name = getattr(self.model, field_name)(self)
            # 添加完毕
            if self.list_editable and item.field and item.field.editable and (field_name in self.list_editable):
    
                pk = getattr(obj, obj._meta.pk.attname)
                field_label = label_for_field(field_name, obj,
                                              model_admin=self.admin_view,
                                              return_attr=False
                                              )
    
                item.wraps.insert(0, '<span class="editable-field">%s</span>')
                item.btns.append((
                                         '<a class="editable-handler" title="%s" data-editable-field="%s" data-editable-loadurl="%s">' +
                                         '<i class="fa fa-edit"></i></a>') %
                                 (_(u"Enter %s") % field_label, field_name,
                                  self.admin_view.model_admin_url('patch', pk) + '?fields=' + field_name))
    
                if field_name not in self.editable_need_fields:
                    self.editable_need_fields[field_name] = item.field
    
            return item
  • 相关阅读:
    50 系统调用的实现
    49 进程调度预备开发(下)
    48 进程调度预备开发(上)
    47 多进程并行执行(下)
    IIC总线
    46 多进程并行执行(上)
    45 内核中的中断处理(下)
    解决错误/usr/bin/ld: cannot find -lz
    rsync只传输隐藏文件
    Firewall命令
  • 原文地址:https://www.cnblogs.com/wtil/p/12528271.html
Copyright © 2020-2023  润新知