12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- # -*- coding: utf-8 -*-
- from .const import get_join
- from .related_field import RelatedField
- class CoModel: # 用于many2one字段的model
- def __init__(self, alias_of_query_model, query_model_field, comodel_name, comodel_field, comodel_show_field,
- get_field_info_func, get_table_func):
- self._alias_of_query_model = alias_of_query_model
- self._query_model_field = query_model_field # many2one field
- self._comodel_name = comodel_name
- self._comodel_table = get_table_func(comodel_name)
- self._comodel_alias = '{}_{}'.format(self._query_model_field, self._comodel_table)
- self._comodel_field = comodel_field
- self._comodel_show_field = comodel_show_field # can be related field
- # ====related field below:====
- # for example:
- # create_uid=fields.Many2one('res.users',...)
- # res.users:
- # name = fields.Char(related='partner_id.name', ...)
- # partner_id = fields.Many2one('res.partner', ...)
- self._is_related = False # True表示是related非store字段,False则不需要处理
- self._related_alias = None # for example: 'create_uid_res_partner'
- self._related_comodel_name = None # for example: 'res.partner'
- self._related_comodel_table = None # for example: 'res_partner'
- self._related_field_in_comodel = None # for example: 'partner_id'
- self._field_in_related_model = None # for example: 'name'(in 'res.partner')
- self._set_related(comodel_name, comodel_show_field, get_field_info_func, get_table_func)
- return
- def __str__(self):
- _f = '<CoModel== left alias:{},left field:{},comodel name:{}, comodel table:{},comodel alias:{},' \
- 'comodel field:{},comodel show:{}, is related:{}, related alias:{}, related comodel name:{},' \
- 'related comodel table:{},related field in comodel:{}, field in related model:{}>'
- return _f.format(self._alias_of_query_model, self._query_model_field, self._comodel_name, self._comodel_table,
- self._comodel_alias, self._comodel_field, self._comodel_show_field, self._is_related,
- self._related_alias, self._related_comodel_name, self._related_comodel_table,
- self._related_field_in_comodel, self._field_in_related_model)
- def get_comodel_name(self):
- return self._comodel_name
- def _set_related(self, comodel_name, comodel_show_field, get_field_info_func, get_table_func):
- # ====related field below:====
- # for example:
- # create_uid=fields.Many2one('res.users',...)
- # res.users:
- # name = fields.Char(related='partner_id.name', ...)
- # partner_id = fields.Many2one('res.partner', ...)
- comodel_show_field_info = get_field_info_func(comodel_name, comodel_show_field)
- if comodel_show_field_info.related_field:
- _related_field_in_comodel = RelatedField(self._alias_of_query_model, comodel_show_field_info.depends[0])
- self._related_field_in_comodel = _related_field_in_comodel.get_related_field() # partner_id
- self._field_in_related_model = _related_field_in_comodel.get_field_in_related_model() # name in partner_id
- _field_in_comodel_info = get_field_info_func(comodel_name,
- self._related_field_in_comodel) # info of partner_id
- if not comodel_show_field_info.store:
- if _field_in_comodel_info.type != 'many2one':
- print('find related field in comodel error')
- else:
- self._is_related = True
- self._related_comodel_name = _field_in_comodel_info.comodel_name
- self._related_comodel_table = get_table_func(self._related_comodel_name)
- self._related_alias = '{}_{}'.format(self._query_model_field, self._related_comodel_table)
- return
- def get_join(self):
- join_exp = get_join(self._alias_of_query_model, self._query_model_field, self._comodel_table,
- self._comodel_alias, self._comodel_field)
- if not self._is_related:
- return join_exp
- related_join = get_join(self._comodel_alias, self._related_field_in_comodel, self._related_comodel_table,
- self._related_alias, 'id')
- return '{} {}'.format(join_exp, related_join)
- def _get_selected_alias(self): # 加不加引号没有影响,字段也是
- return self._related_alias if self._is_related else self._comodel_alias
- def _get_selected_field(self):
- return self._field_in_related_model if self._is_related else self._comodel_show_field
- def get_selected_field(self):
- return '%s.%s' % (self._get_selected_alias(), self._get_selected_field())
- def get_alias(self):
- return self._get_selected_alias()
- def get_table(self):
- return self._related_comodel_table if self._is_related else self._comodel_table
|