简体   繁体   中英

Odoo 10 - Retrieve the value of a field of another model

Here is code:

Target model:

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)

Targeted fields: -> year_begin_date = fields.Date(string='Start date', required=True, copy=False)

Model where I want to access the fields:

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)
...

Here is the view: 在此处输入图片说明 I want to get the correct start date of the school year related to school_years which is of type char and which is a Many2one in the model ecole.partner.school. I know there are many ways to do this, especially with a "related field". Except that I have a function that allows me to recover the date of the day as of the beginning of the school year when one is in full in a school period. Currently my function is written in "hard" -> this is what we see in the variable "domain". And I do not want to use "related field" in my school_registration field.

Do you have an idea to get the right start date when choosing a school year?

Thank you

You can try with a computed fields :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM