在我们的团队中,我们希望实现这样一个特性,即每当用户正在更新的记录自他/她打开记录以来,他/她的一个学院也会对他/她进行警告。
我们挖掘源代码是因为我们没有找到任何正式文档,只有一些不符合我们的Odoo版本(11)的模块。
我们在文件/odoo/odoo/models.py中找到了方法def _check_concurrency(self):,其代码如下:
@api.multi
def _check_concurrency(self):
if not (self._log_access and self._context.get(self.CONCURRENCY_CHECK_FIELD)):
return
check_clause = "(id = %s AND %s < COALESCE(write_date, create_date, (now() at time zone 'UTC'))::timestamp)"
for sub_ids in self._cr.split_for_in_conditions(self.ids):
nclauses = 0
params = []
for id in sub_ids:
id_ref = "%s,%s" % (self._name, id)
update_date = self._context[self.CONCURRENCY_CHECK_FIELD].pop(id_ref, None)
if update_date:
nclauses += 1
params.extend([id, update_date])
if not nclauses:
continue
query = "SELECT id FROM %s WHERE %s" % (self._table, " OR ".join([check_clause] * nclauses))
self._cr.execute(query, tuple(params))
res = self._cr.fetchone()
if res:
# mention the first one only to keep the error message readable
raise ValidationError(_('A document was modified since you last viewed it (%s:%d)') % (self._description, res[0]))=>在任何“写”之前调用此方法。它比较:
__last_update发生时的write值__last_update值,因此应该事先在context中设置问题
我们没有在代码(python或javascript)中找到上下文中设置的值,=>中什么都没有发生!THe函数从一开始就返回。
当我们试图在上下文中对其进行硬编码时,函数check_concurrency似乎工作正常。
问题
有谁知道在上下文中设置或应该设置__last_update的位置吗?怎么做呢?例如,我可以想象在单击记录的编辑按钮时,以某种方式设置它?还是在读书的时候?
发布于 2018-06-06 12:26:50
CONCURRENCY_CHECK_FIELD = '__last_update'并发字段是一个动态字段,计算方法是动态定义的,您还可以看到last_modified_name = 'compute_concurrency_field_with_access'或last_modified_name = 'compute_concurrency_field'根据access进行更新,然后添加到类中。以下功能将参与解决方案。
@api.model
def _add_magic_fields(self):
""" Introduce magic fields on the current class
* id is a "normal" field (with a specific getter)
* create_uid, create_date, write_uid and write_date have become
"normal" fields
* $CONCURRENCY_CHECK_FIELD is a computed field with its computing
method defined dynamically. Uses ``str(datetime.datetime.utcnow())``
to get the same structure as the previous
``(now() at time zone 'UTC')::timestamp``::
# select (now() at time zone 'UTC')::timestamp;
timezone
----------------------------
2013-06-18 08:30:37.292809
>>> str(datetime.datetime.utcnow())
'2013-06-18 08:31:32.821177'
"""
def add(name, field):
""" add ``field`` with the given ``name`` if it does not exist yet """
if name not in self._fields:
self._add_field(name, field)
# cyclic import
from . import fields
# this field 'id' must override any other column or field
self._add_field('id', fields.Id(automatic=True))
add('display_name', fields.Char(string='Display Name', automatic=True,
compute='_compute_display_name'))
if self._log_access:
add('create_uid', fields.Many2one('res.users', string='Created by', automatic=True))
add('create_date', fields.Datetime(string='Created on', automatic=True))
add('write_uid', fields.Many2one('res.users', string='Last Updated by', automatic=True))
add('write_date', fields.Datetime(string='Last Updated on', automatic=True))
last_modified_name = 'compute_concurrency_field_with_access'
else:
last_modified_name = 'compute_concurrency_field'
# this field must override any other column or field
self._add_field(self.CONCURRENCY_CHECK_FIELD, fields.Datetime(
string='Last Modified on', compute=last_modified_name, automatic=True))
def compute_concurrency_field(self):
for record in self:
record[self.CONCURRENCY_CHECK_FIELD] = odoo.fields.Datetime.now()
@api.depends('create_date', 'write_date')
def compute_concurrency_field_with_access(self):
for record in self:
record[self.CONCURRENCY_CHECK_FIELD] = \
record.write_date or record.create_date or odoo.fields.Datetime.now()https://stackoverflow.com/questions/50685432
复制相似问题