代码之家  ›  专栏  ›  技术社区  ›  Dhouha

如何从另一个多个字段中获取字段值?

  •  1
  • Dhouha  · 技术社区  · 6 年前

    我使用odoo 10,并创建了一个定制模块来规划行程。我为计划创建一个视图,在其中我将选择我的旅行列表。我现在的问题是,如果知道显示行程列表的字段是 旅行ID=字段。许多('tms.travel',copy=False,string=Travels') .我试了很多,但没有结果。想帮忙吗??

    enter image description here

    旅游规划。xml

    <?xml version="1.0" encoding="UTF-8"?>
    <odoo>
    <data>
    
        <record id="view_tms_travel_planning_form" model="ir.ui.view">
            <field name="name">view.tms.travel.planning.form</field>
            <field name="model">tms.planning</field>
            <field name="arch" type="xml">
                <form string="Plannification des voyage">
                    <header>
                        <field name="state" statusbar_visible="draft,approved,confirmed" widget="statusbar"/>
                    </header>
                    <sheet>
                    <div class="oe_title">
                        <h1>
                            <label class="oe_inline" style="font-size:30px;" string="Plannification - " attrs="{'invisible':[('name','=', False)]}"/>
                            <field name="name" readonly="1"/>
                        </h1>
                    </div>
                    <group>
                        <group>
    
                            <field name="datetime"/>
                            <field attrs="{'readonly':[('state','in',('confirmed', 'cancel'))]}" name="operating_unit_id"/>
                        </group>
    
                        <group>
                            <field name="num_vehicule_dispo"/>
                            <field name="num_chauffeur_dispo"/>
    
                        </group>
                    </group>
                    <notebook colspan="1">
                        <page string="Les voyages à planifier">
                            <separator coslpan="4" string="Voyages"/>
                            <!--<field attrs="{'readonly':[('state','in',('confirmed', 'cancel'))]}" name="operating_unit_id"/>-->
                            <field attrs="{'readonly':[('state','in',('confirmed', 'cancel'))]}" colspan="4"
                                   domain="[('state','not in',('cancel','closed'))]" name="travel_ids" nolabel="1"/>
                            <separator coslpan="4" string="Véhicules"/>
                            <field  colspan="4" name="fleet_ids" nolabel="1"/>
                            <separator coslpan="4" string="Conducteurs"/>
                            <field  colspan="4" name="employee_ids" nolabel="1"/>
    
                        </page>
    
                    </notebook>
                    </sheet>
                </form>
            </field>
        </record>
    </data>
     </odoo>
    

    旅游规划。py

     # -*- coding: utf-8 -*-
    
    from odoo import _, api, fields, models
    from odoo.exceptions import ValidationError
    
    
    class TmsTravelPlanning(models.Model):
    _name = 'tms.planning'
    
    name = fields.Char('Num du plannification')
    operating_unit_id = fields.Many2one(
        'operating.unit', string='Operating Unit', required=True)
    id_planning=fields.Integer(string="Numéro du plannification")
    datetime=fields.Date(string="Date")
    num_vehicule_dispo=fields.Integer(string="Nombre de véhicule disponible")
    num_chauffeur_dispo=fields.Integer(string="Nombre de chauffeur disponible")
    tms
    fleet_ids = fields.Many2many('fleet.vehicle', copy=False, string='Véhicules')
    employee_ids = fields.Many2many('hr.employee', copy=False, string='Conducteurs')
    
    state = fields.Selection([
        ('draft', 'Pending'),
        ('approved', 'Approved'),
        ('confirmed', 'Confirmed'),
        ('cancel', 'Cancelled')], readonly=True,
        help="Gives the state of the Waybill.",
        default='draft')
    
    @api.model
    def create(self, values):
        planning = super(TmsTravelPlanning, self).create(values)
        if not planning.operating_unit_id.planning_sequence_id:
            raise ValidationError(_(
                'You need to define the sequence for planning in base %s' %
                planning.operating_unit_id.name
            ))
        sequence = planning.operating_unit_id.planning_sequence_id
        planning.name = sequence.next_by_id()
        print(str(values['num_vehicule_dispo']))
        print(str(values['num_chauffeur_dispo']))
        return planning
    

    tms_旅行社。xml

        <?xml version="1.0" encoding="UTF-8"?>
        <odoo>
        <record id="view_tms_travel_tree" model="ir.ui.view">
              <field name="name">tms.travel.tree</field>
                <field name="model">tms.travel</field>
             <field name="priority">1</field>
             <field name="arch" type="xml">
                <tree>
                     <field name="name"/>
                    <field name="date"/>
                  <field name="departure_id"/>
                     <field name="arrival_id"/>
                      <field name="state"/>
                     <button groups="tms.group_traffic,tms.group_expenses" icon="fa-thumbs-up" name="action_progress" states="draft" string="Dispatch Travel" type="object"/>
                <button groups="tms.group_traffic,tms.group_expenses" icon="fa-check-square" name="action_end" states="progress" string="End Travel" type="object"/>
            </tree>
        </field>
    </record>
    </odoo>
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Cristin Meravi    6 年前

    好吧,你在你众多的关系中遗漏了一些信息。这是你需要的。

    field_name = fields.Manmy2many('related.model', 'relational_table', 'current_model_id', 'related_model_id', string='other information')
    

    通常我会在相关的模型上加上反比,比如:

    (关于hr.holidays模型)

    payslip_ids = fields.Many2many('hr.payslip', 'hr_payslip_holiday_rel', 'holiday_id', 'payslip_id', ...)
    

    (在人力资源工资单模型上)

    holiday_ids - fields.Many2many('hr.holidays', 'hr_payslip_holiday_rel', 'payslip_id', 'holiday_id', ...)
    

    然后,在某个时刻,您需要将其中一个ID添加到另一个模型中,以便执行以下操作:

    holiday.payslip_ids |= current_payslip_id