co_model.py 5.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # -*- coding: utf-8 -*-
  2. from .const import get_join
  3. from .related_field import RelatedField
  4. class CoModel: # 用于many2one字段的model
  5. def __init__(self, alias_of_query_model, query_model_field, comodel_name, comodel_field, comodel_show_field,
  6. get_field_info_func, get_table_func):
  7. self._alias_of_query_model = alias_of_query_model
  8. self._query_model_field = query_model_field # many2one field
  9. self._comodel_name = comodel_name
  10. self._comodel_table = get_table_func(comodel_name)
  11. self._comodel_alias = '{}_{}'.format(self._query_model_field, self._comodel_table)
  12. self._comodel_field = comodel_field
  13. self._comodel_show_field = comodel_show_field # can be related field
  14. # ====related field below:====
  15. # for example:
  16. # create_uid=fields.Many2one('res.users',...)
  17. # res.users:
  18. # name = fields.Char(related='partner_id.name', ...)
  19. # partner_id = fields.Many2one('res.partner', ...)
  20. self._is_related = False # True表示是related非store字段,False则不需要处理
  21. self._related_alias = None # for example: 'create_uid_res_partner'
  22. self._related_comodel_name = None # for example: 'res.partner'
  23. self._related_comodel_table = None # for example: 'res_partner'
  24. self._related_field_in_comodel = None # for example: 'partner_id'
  25. self._field_in_related_model = None # for example: 'name'(in 'res.partner')
  26. self._set_related(comodel_name, comodel_show_field, get_field_info_func, get_table_func)
  27. return
  28. def __str__(self):
  29. _f = '<CoModel== left alias:{},left field:{},comodel name:{}, comodel table:{},comodel alias:{},' \
  30. 'comodel field:{},comodel show:{}, is related:{}, related alias:{}, related comodel name:{},' \
  31. 'related comodel table:{},related field in comodel:{}, field in related model:{}>'
  32. return _f.format(self._alias_of_query_model, self._query_model_field, self._comodel_name, self._comodel_table,
  33. self._comodel_alias, self._comodel_field, self._comodel_show_field, self._is_related,
  34. self._related_alias, self._related_comodel_name, self._related_comodel_table,
  35. self._related_field_in_comodel, self._field_in_related_model)
  36. def get_comodel_name(self):
  37. return self._comodel_name
  38. def _set_related(self, comodel_name, comodel_show_field, get_field_info_func, get_table_func):
  39. # ====related field below:====
  40. # for example:
  41. # create_uid=fields.Many2one('res.users',...)
  42. # res.users:
  43. # name = fields.Char(related='partner_id.name', ...)
  44. # partner_id = fields.Many2one('res.partner', ...)
  45. comodel_show_field_info = get_field_info_func(comodel_name, comodel_show_field)
  46. if comodel_show_field_info.related_field:
  47. _related_field_in_comodel = RelatedField(self._alias_of_query_model, comodel_show_field_info.depends[0])
  48. self._related_field_in_comodel = _related_field_in_comodel.get_related_field() # partner_id
  49. self._field_in_related_model = _related_field_in_comodel.get_field_in_related_model() # name in partner_id
  50. _field_in_comodel_info = get_field_info_func(comodel_name,
  51. self._related_field_in_comodel) # info of partner_id
  52. if not comodel_show_field_info.store:
  53. if _field_in_comodel_info.type != 'many2one':
  54. print('find related field in comodel error')
  55. else:
  56. self._is_related = True
  57. self._related_comodel_name = _field_in_comodel_info.comodel_name
  58. self._related_comodel_table = get_table_func(self._related_comodel_name)
  59. self._related_alias = '{}_{}'.format(self._query_model_field, self._related_comodel_table)
  60. return
  61. def get_join(self):
  62. join_exp = get_join(self._alias_of_query_model, self._query_model_field, self._comodel_table,
  63. self._comodel_alias, self._comodel_field)
  64. if not self._is_related:
  65. return join_exp
  66. related_join = get_join(self._comodel_alias, self._related_field_in_comodel, self._related_comodel_table,
  67. self._related_alias, 'id')
  68. return '{} {}'.format(join_exp, related_join)
  69. def _get_selected_alias(self): # 加不加引号没有影响,字段也是
  70. return self._related_alias if self._is_related else self._comodel_alias
  71. def _get_selected_field(self):
  72. return self._field_in_related_model if self._is_related else self._comodel_show_field
  73. def get_selected_field(self):
  74. return '%s.%s' % (self._get_selected_alias(), self._get_selected_field())
  75. def get_alias(self):
  76. return self._get_selected_alias()
  77. def get_table(self):
  78. return self._related_comodel_table if self._is_related else self._comodel_table