Jean Pernier
Jean Pernier

Reputation: 13

how to compare a date field to the current date in field access rule [odoo 10]

My record can only be modified before a given date (in a field).

I thought about using record rules but can't compare a date field to the current date in access rule.

Any way to do the comparison or another way to achieve this?

<record id="my_rule_date_foo" model="ir.rule">
<field name="name">foo bar</field>
<field name="model_id" ref="model_my_foo"/>
<field name="domain_force">
    [('many2one_field.the_limit_date','&ge;', 'what_to_put_here_?')]
</field>
<field name="groups" eval="[(4, ref('group_peff'))]"/>
<field name="perm_read" eval="False"/>
<field name="perm_write" eval="True"/>
<field name="perm_create" eval="False"/>
<field name="perm_unlink" eval="False"/>

Upvotes: 1

Views: 913

Answers (1)

Travis Waelbroeck
Travis Waelbroeck

Reputation: 2135

You should be able to use context_today() or datetime in the domain_force:

# Should be the best choice, considers user timezone
[('many2one_field.the_limit_date','&gt;=', context_today())]

# If context_today() doesn't work for some reason, you can use datetime
[('many2one_field.the_limit_date','&gt;=', datetime.date.today().strftime('%Y-%m-%d'))]

Here's the context_today method from core and a relevant gist for reference.

Upvotes: 2

Related Questions