这是代码:
目标模型:
class SchoolYears(models.Model):
_name = "ecole.partner.school.years"
_rec_name = "school_years" # POUR ASSIGNER PAR DEFAUT UN AUTRE CHAMP AUTRE QUE NAME
_order = 'id desc'
school_years = fields.Char(string='School year', required=True, copy=False)
year_begin_date = fields.Date(string='Start date', required=True, copy=False)
year_end_date = fields.Date(string='End date', required=True, copy=False)
default_school_year = fields.Boolean(string='Current school year', copy=False)
period_school_year = fields.Boolean(string='Registration period', copy=False)
active = fields.Boolean(default=True)
目标字段:-> year_begin_date =字段。日期(字符串=“开始日期”,必填=真,复制=假)
要在其中访问字段的模型:
class ResPartnerSchool(models.Model):
_name = 'ecole.partner.school'
_order = 'id desc'
@api.multi
def _get_begin_date(self):
domain = [('period_school_year', '=', False), ('default_school_year', '=', True)]
begin_date_id = self.env['ecole.partner.school.years'].search(domain, limit=1).year_begin_date
begin_date = fields.Date.from_string(begin_date_id)
date_j = datetime.date.today()
if begin_date_id:
if begin_date > date_j:
return begin_date_id
else:
return date_j
...
school_year_id = fields.Many2one(string='Period',
ondelete='SET NULL',
comodel_name="ecole.partner.school.years",
default=_get_period_year)
school_registration = fields.Date(string='Beginning',
copy=False,
default=_get_begin_date)
...
以下是视图: 我想获得与school_years有关的学年的正确开始日期,该日期是char类型,并且在模型ecole.partner.school中是Many2one。 我知道有很多方法可以做到这一点,特别是在“相关领域”。除了我的功能可以让我恢复上学期满的学年开始之日的日期。 目前,我的函数是用“ hard”编写的->这就是我们在变量“ domain”中看到的内容。而且我不想在我的school_registration字段中使用“相关字段”。
您是否有选择正确的开学日期的想法?
谢谢
答案 0 :(得分:0)
您可以尝试使用计算字段:
school_year_id = fields.Many2one(string='Period',
ondelete='SET NULL',
comodel_name="ecole.partner.school.years",
default=_get_period_year)
school_registration = fields.Date(string='Beginning',
copy=False,
compute=_get_begin_date)
@api.multi
@api.depends('school_year_id')
def _get_begin_date(self):
for record in self:
record.school_registration = record.school_year_id.year_begin_date