콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
1 회신
112 화면

How do you people usually do dynamic form view for repetitive fields

Like i have a tyre management system and lets say at slot_1 has slot_1_brand, _size, _otd, _rtd, _condition, etc.

Do I have to do that for every single tyre statically if i specifically, SPECIFICALLY, want to show it in form view?

All the guides been telling me to just do usual one2many and show it in tree list. But I want it in form view, and it has to be able to have readonly and invisible conditioning plus buttons repeated for every single line of data of the tyre.slot model in form view

So please tell me how do I do this, thank you so much in advance for anyone answering 

아바타
취소

Do you mean to add an undefined number of fields in the form? Like slot_1_brand, slot_2_brand, slot_n_brand where n is to be determined by *something*? What is the outcome you are looking for and why exactly is a One2many field not suitable? Because a One2many field is supposed to exactly handle this scenario... Also, One2many lists in forms allow for readonly, invisible (and column_invisible) conditions as well. So far it sounds like really bad model design.

베스트 답변

Hi,


In Odoo, when you have repetitive data like tyre slots, the recommended approach is to create a tyre.slot model and link it to your main model (e.g., vehicle) via a One2many field. Then, in the form view, you can display each tyre slot in a form view inside a notebook or a group.


Code:

from odoo import models, fields, api


class Vehicle(models.Model):

    _name = 'fleet.vehicle'

    _description = 'Vehicle'


    name = fields.Char(string="Vehicle Name")

    tyre_slot_ids = fields.One2many('tyre.slot', 'vehicle_id', string="Tyre Slots")



class TyreSlot(models.Model):

    _name = 'tyre.slot'

    _description = 'Tyre Slot'


    vehicle_id = fields.Many2one('fleet.vehicle', string="Vehicle")

    slot_number = fields.Integer(string="Slot Number")

    brand = fields.Char(string="Brand")

    size = fields.Char(string="Size")

    otd = fields.Float(string="OTD")

    rtd = fields.Float(string="RTD")

    condition = fields.Selection([('new', 'New'), ('used', 'Used'), ('bad', 'Bad')], string="Condition")


    def button_repair(self):

        # Example button logic

        self.condition = 'new'


XML:

<record id="view_vehicle_form" model="ir.ui.view">

    <field name="name">fleet.vehicle.form</field>

    <field name="model">fleet.vehicle</field>

    <field name="arch" type="xml">

        <form string="Vehicle">

            <sheet>

                <group>

                    <field name="name"/>

                </group>

                <notebook>

                    <page string="Tyre Slots">

                        <field name="tyre_slot_ids">

                            <form>

                                <group>

                                    <field name="slot_number"/>

                                    <field name="brand"/>

                                    <field name="size"/>

                                    <field name="otd"/>

                                    <field name="rtd"/>

                                    <field name="condition" attrs="{'readonly':[('condition','=','bad')]}"/>

                                </group>

                                <footer>

                                    <button name="button_repair" type="object" string="Repair" class="btn-primary"/>

                                </footer>

                            </form>

                        </field>

                    </page>

                </notebook>

            </sheet>

        </form>

    </field>

</record>


Hope it helps

아바타
취소
관련 게시물 답글 화면 활동
2
10월 25
268
4
10월 25
2869
2
9월 25
1834
4
9월 25
6943
0
8월 25
879