如何在 odoo datepicker 中禁止当前日期后的天数?

问题描述 投票:1回答:1

我有一个带日期字段的视图。用户不能选择今天之后的日期。我如何在Odoo datepicker中禁用当前日期之后的所有日期?

python odoo
1个回答
1
投票

日期选择器 maxDate 用于设置最大可选择的日期,这将使当前日期之后的日子无法使用。

我没有找到从XML中动态传递当前日期的方法,所以我覆盖了 fields_view_get 改变 options 属性,并设置 maxDate 为当前日期。

下面的例子将maxDate选项设置为 date_invoice:

class AccountInvoice(models.Model):
    _inherit = 'account.invoice'

    @api.model
    def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
        res = super(AccountInvoice, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu)
        if view_type == 'form':
            doc = etree.XML(res['arch'])
            for node in doc.xpath("//field[@name='date_invoice']"):
                node.set('options', "{'datepicker': {'maxDate': '%sT23:59:59'}}" % fields.Date.today().strftime(DEFAULT_SERVER_DATE_FORMAT))
            res['arch'] = etree.tostring(doc)
        return res

编辑。

你可以使用 or 在XPATH表达式中指定第二个字段名。

doc.xpath("//field[@name='date_invoice' or @name='date_due']")

编辑。

尝试指定一个时间 23:59:59

node.set('options', "{'datepicker': {'maxDate': '%sT23:59:59'}}" % fields.Date.today().strftime(DEFAULT_SERVER_DATE_FORMAT))
© www.soinside.com 2019 - 2024. All rights reserved.