attributeError: 'str' object has no attribute 'dbname'

attributeError: 'str' object has no attribute 'dbname'

我创建了一个新模块来重写 _read_group_process_groupby 方法 来自 server\openerp\modules.py 并添加按小时分组的选项, 但我得到这个错误:

我模块中的方法 \addons-custom\my_group_hours\models\models.py 是下面

@api.model
def _read_group_process_groupby(self, gb, query, context):

   split = gb.split(':')
   field_type = self._fields[split[0]].type
   gb_function = split[1] if len(split) == 2 else None
   temporal = field_type in ('date', 'datetime')
   tz_convert = field_type == 'datetime' and context.get('tz') in pytz.all_timezones
   qualified_field = self._inherits_join_calc(self._table, split[0], query)
   if temporal:
       display_formats = {
           'hour': 'HH:mm dd MMM yyyy',
           'day': 'dd MMM yyyy',
           'week': "'W'w YYYY", 
           'month': 'MMMM yyyy',
           'quarter': 'QQQ yyyy',
           'year': 'yyyy',
       }
   time_intervals = {
       'hour': dateutil.relativedelta.relativedelta(hours=1),
       'day': dateutil.relativedelta.relativedelta(days=1),
       'week': datetime.timedelta(days=7),
       'month': dateutil.relativedelta.relativedelta(months=1),
       'quarter': dateutil.relativedelta.relativedelta(months=3),
       'year': dateutil.relativedelta.relativedelta(years=1)
   }
   if tz_convert:
           qualified_field = "timezone('%s', timezone('UTC',%s))" % (context.get('tz', 'UTC'), qualified_field)
       qualified_field = "date_trunc('%s', %s)" % (gb_function or 'month', qualified_field)
   if field_type == 'boolean':
       qualified_field = "coalesce(%s,false)" % qualified_field
   return {
       'field': split[0],
       'groupby': gb,
       'type': field_type,
       'display_format': display_formats[gb_function or 'month'] if temporal else None,
       'interval': time_intervals[gb_function or 'month'] if temporal else None,
       'tz_convert': tz_convert,
       'qualified_field': qualified_field
   }

models.BaseModel._read_group_process_groupby = _read_group_process_groupby

@api.model 用于向旧 API 公开一种新式方法。

调用您的方法时,gb 将作为 cr 传递,结果为:

AttributeError: 'str' object has no attribute 'dbname'

您只需要删除装饰器。