Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
1 Odpowiedz
137 Widoki

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 

Awatar
Odrzuć

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.

Najlepsza odpowiedź

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

Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
2
paź 25
300
4
paź 25
2917
2
wrz 25
1855
4
wrz 25
6968
0
sie 25
890