123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- # -*- coding: utf-8 -*-
- from .const import get_join
- class RelatedField:
- # 举例:
- # customer_name = fields.Char(string='客户', related='customer_id.name')
- # 则:
- # for field, info in self._fields.items():
- # info.related: ('customer','name')
- # info.related_field: 'archives.customer.name'
- # info.depends: ('customer_id.name',)
- def __init__(self, left_alias, related_info):
- self._left_alias = left_alias
- self._related_info = related_info
- self._related_field, self._related_model_field = RelatedField._get_related_info(related_info)
- self._related_model = None
- return
- def __str__(self):
- _f = '<RelatedField== left alias:{}, related model:{},related field:{},related model field:{}>'
- return _f.format(self._left_alias, self._related_model, self._related_field, self._related_model_field)
- def get_related_field(self):
- return self._related_field
- def get_field_in_related_model(self):
- return self._related_model_field
- def set_related_model(self, related_model):
- self._related_model = related_model
- return
- def get_related_alias(self):
- return self._related_model.get_alias()
- def get_join(self):
- return get_join(self._left_alias, self._related_field, self._related_model.get_table(),
- self._related_model.get_alias(), 'id')
- def get_selected_field(self):
- return '{}.{}'.format(self._related_model.get_alias(), self._related_model_field)
- @staticmethod
- def _get_related_info(related_field):
- # print('_get_related_info, related_field:', related_field)
- if not related_field:
- return '', ''
- pair = related_field.split('.')
- if len(pair) == 2:
- return pair
- return '', ''
|