ORM-1 字段默认值

  当在odoo页面新建一条记录的时候,前端会通过rpc调用后端模型的default_get 函数,根据前端传入的字段列表,来获取相对应的字段默认值。
	# odoo/models.py
    @api.model
    def default_get(self, fields_list):
        """ default_get(fields_list) -> default_values

        Return default values for the fields in ``fields_list``. Default
        values are determined by the context, user defaults, and the model
        itself.

        :param list fields_list: names of field whose default is requested
        :return: a dictionary mapping field names to their corresponding default values,
            if they have a default value.
        :rtype: dict

        .. note::

            Unrequested defaults won't be considered, there is no need to return a
            value for fields whose names are not in `fields_list`.
		
        """
        defaults = {}
        parent_fields = defaultdict(list)
        ir_defaults = self.env['ir.default'].get_model_defaults(self._name)

        for name in fields_list:
            # 1. look up context
            key = 'default_' + name
            if key in self._context:
                defaults[name] = self._context[key]
                continue

            # 2. look up ir.default
            if name in ir_defaults:
                defaults[name] = ir_defaults[name]
                continue

            field = self._fields.get(name)

            # 3. look up field.default
            if field and field.default:
                defaults[name] = field.default(self)
                continue

            # 4. delegate to parent model
            if field and field.inherited:
                field = field.related_field
                parent_fields[field.model_name].append(field.name)

        # convert default values to the right format
        #
        # we explicitly avoid using _convert_to_write() for x2many fields,
        # because the latter leaves values like [(Command.LINK, 2),
        # (Command.LINK, 3)], which are not supported by the web client as
        # default values; stepping through the cache allows to normalize
        # such a list to [(Command.SET, 0, [2, 3])], which is properly
        # supported by the web client
        for fname, value in defaults.items():
            if fname in self._fields:
                field = self._fields[fname]
                value = field.convert_to_cache(value, self, validate=False)
                defaults[fname] = field.convert_to_write(value, self)

        # add default values for inherited fields
        for model, names in parent_fields.items():
            defaults.update(self.env[model].default_get(names))

        return defaults

1、该函数的作用

    函数文档说的很清楚了,我在赘述一遍。
    该函数根据传入的字段名返回响应的默认值,默认值是由context,用户定义以及模型本身决定的。
    输入参数: field_list  字段名称列表。
    返回值: 一个字典,key是字段名称,value是该字段的默认值
    注意: 该函数只返回请求列表中的字段的默认值。至于有哪些字段,是由前端视图决定的。

2、设置默认值的四种方式

从代码中可以看到,获取字典的默认值,按照优先级有四种方式:

2.1 上下文context

action

        <record id="action_move_out_invoice_type" model="ir.actions.act_window">
            <field name="name">Invoices</field>
            <field name="res_model">account.move</field>
            <field name="view_mode">tree,kanban,form</field>
            <field name="view_id" ref="view_out_invoice_tree"/>
            <field name="search_view_id" ref="view_account_invoice_filter"/>
            <field name="domain">[('move_type', '=', 'out_invoice')]</field>
            <field name="context">{'default_move_type': 'out_invoice'}</field>
            <field name="help" type="html">
              <p class="o_view_nocontent_smiling_face">
                Create a customer invoice
              </p><p>
                Create invoices, register payments and keep track of the discussions with your customers.
              </p>
            </field>
        </record>

2.2 ir.default

@api.model
    def get(self, model_name, field_name, user_id=False, company_id=False, condition=False):
        """ Return the default value for the given field, user and company, or
            ``None`` if no default is available.

            :param model_name:
            :param field_name:
            :param user_id: may be ``False`` for all users, ``True`` for the
                            current user, or any user id
            :param company_id: may be ``False`` for all companies, ``True`` for
                               the current user's company, or any company id
            :param condition: optional condition that restricts the
                              applicability of the default value; this is an
                              opaque string, but the client typically uses
                              single-field conditions in the form ``'key=val'``.
        """
        if user_id is True:
            user_id = self.env.uid
        if company_id is True:
            company_id = self.env.company.id

        field = self.env['ir.model.fields']._get(model_name, field_name)
        default = self.search([
            ('field_id', '=', field.id),
            ('user_id', '=', user_id),
            ('company_id', '=', company_id),
            ('condition', '=', condition),
        ], limit=1)
        return json.loads(default.json_value) if default else None

从代码看, 可以对模型中的某一字典,根据不同的用户、公司、条件设置不同的默认值。
但是实际开发中很少用。

2.3 字段的默认值

这是最常见的设置默认值的一种方式
有三种设置方法:
1、 直接设置

active = fields.Boolean(default=True)

2、设置为一个函数

example_date = fields.Date(string='Date example', default=_default_example_date, store=False)

3 设置为lamda函数

 user_id = fields.Many2one('res.users', default= lambda self: self.env.user) 

2.4 通过委托继承的父表的默认值(_inherits)

关于委托继承,这里就不具体描述了。 简单理解就是数据存储在父表中,但是可以当自己的字段用。
通过一个many2one字段进行关联。

3 重写default_get

某些场景下,我们可以model中重写这个函数,加上我们自己的逻辑,比如:

    def default_get(self, fields_list):
    	# 先调用父类方法
        defaults = super().default_get(fields_list)
		# 然后再执行自己的逻辑去对defaults 进行修改,最后再将它返回。
        if 'journal_id' in defaults and 'date' in fields_list:
            last_line = self.search([
                ('journal_id', '=', defaults.get('journal_id')),
                ('state', '=', 'posted'),
            ], limit=1)
            statement = last_line.statement_id
            if statement:
                defaults.setdefault('date', statement.date)
            elif last_line:
                defaults.setdefault('date', last_line.date)

        return defaults

# 高校智慧校园解决方案摘要 智慧校园解决方案是针对高校信息化建设的核心工程,旨在通过物联网技术实现数字化校园的智能化升级。该方案通过融合计算机技术、网络通信技术、数据库技术和IC卡识别技术,初步实现了校园一卡通系统,进而通过人脸识别技术实现了更精准的校园安全管理、生活管理、教务管理和资源管理。 方案包括多个管理系统:智慧校园管理平台、一卡通卡务管理系统、一卡通人脸库管理平台、智能人脸识别消费管理系统、疫情防控管理系统、人脸识别无感识别管理系统、会议签到管理系统、人脸识别通道管理系统和图书馆对接管理系统。这些系统共同构成了智慧校园的信息化基础,通过统一数据库和操作平台,实现了数据共享和信息一致性。 智能人脸识别消费管理系统通过人脸识别终端,在无需接触的情况下快速完成消费支付过程,提升了校园服务效率。疫情防控管理系统利用热成像测温技术、视频智能分析等手段,实现了对校园人员体温监测和疫情信息实时上报,提高了校园公共卫生事件的预防和控制能力。 会议签到管理系统和人脸识别通道管理系统均基于人脸识别技术,实现了会议的快速签到和图书馆等场所的高效通行管理。与图书馆对接管理系统实现了一卡通系统与图书馆管理系统的无缝集成,提升了图书借阅的便捷性。 总体而言,该智慧校园解决方案通过集成的信息化管理系统,提升了校园管理的智能化水平,优化了校园生活体验,增强了校园安全,并提高了教学和科研的效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值