Search code examples
odooodoo-14

How to loop for each record in One2Many field in form view to create dynamic elements?


I have a customized One2Many field that contains several records for this current data.

I am working on a form view.

I need to construct something like this: (I don't want a tree)

<for every record in one2many_field>
  <button something...>
</for>

I only want the result as to create 1 button per record in the one2many_field and be able to get the data in some fields from the model of the one2many_field too.

Is there any way to achieve it? I have searched for some time but found nothing even remotely close to this requirement.


Solution

  • You won't be able to achieve that in a Form View. Views in Odoo are kind of strict but it's mostly to avoid bad stuff to happen.

    Anyway, you could think about re-creating the full view by yourself using QWeb View (doc for v14 here)

    If you want to keep your Form View, you'll have to use a tree inside the form. Know that you can totally add buttons in Tree views like adding on a sale.order.line something like <button class="btn btn-primary" type="object" name="product_id_change" string="Force Product Id Change"/> (useless, just for the example)

    Check example image here

    And play with some attrs to change the visibility of fields...

    Hope it helped :)