'Add one2many field to a website form odoo 13
I have a web form it contains many input fields. I want to add an one2many field to it so that the user can select multiple lines and save them to DB. I want some like the one in this pic 
here is my code for the form :
<template id="create_operation" name="Create Operation">
<t t-call="website.layout">
<div id="wrap">
<div class="oe_structure">
<div class="container">
<form role="form" action="/create/weboperation" method="POST">
<input type="hidden" name="csrf_token" t-att-value="request.csrf_token()"/>
<div class="form-group">
<label for="name" class="control-label">Name</label>
<input type="text" name="name" t-att-value="name" id="name"
class="form-control" placeholder="e.g. John Doe" required="required"/>
</div>
<div class="form-group">
<label for="email" class="control-label">Email</label>
<input type="email" name="email_id" t-att-value="email_id" id="email_id"
class="form-control" required="required"/>
</div>
<div class="form-group">
<label for="picking_type_id" class="control-label">Operation Type</label>
<select name="picking_type_id" class="form-control link-style">
<t t-foreach='picking_rec' t-as="picking">
<option t-esc='picking.name' t-att-value='picking.id'/>
</t>
</select>
</div>
<div class="clearfix oe_login_buttons">
<button type="submit" class="btn btn-primary pull-left">Create Patient</button>
</div>
</form>
</div>
</div>
</div>
</t>
</template>
Solution 1:[1]
First of all, you should create your custom one2many field on the right model : an existing one : product.template ? or a new one : my_model....
If you want to make it available in a webform (frontend template), you can t just use it as one2many field (not the same way as in backend templates), you must use html input fields :
<table>
<thead><th>name</th><th>email</th><th>name</th>picking type</thead>
<tbody>
<t t-foreach=ā[1,2,3]ā t-as=āiā>
<tr>
<td><input type="text" t-att-value="" t-att-id="'name-'+str(i)" t-att-name="'name-'+str(i)" /></td>
<td><input type="text" t-att-value="" t-att-id="'email-'+str(i)" t-att-name="'email-'+str(i)" /></td>
<td><select t-att-id="'picking_type_id-'+str(i)" t-att-name="'picking_type_id-'+str(i)"><option>...</option></select></td>
</tr>
</t>
</tbody>
</table>
Then you should create your custom controller file (/your_module/controllers/main.py), for instance :
# A brand new one only for your model and its fields
class MyOwnModelController(http.Controller):
# A one inheriting the controller of an existing model
class MyWebsiteSale(WebsiteSale):
And then create the "route" and the "def" corresponding to your form action (/create/weboperation) in this main.py file :
@http.route(["/create/weboperation"], type="http", methods=["GET", "POST"], auth="public", website=True, sitemap=False)
def create_webop(self, qcontext):
values['picking_type_id'] = qcontext.get('picking_type_id')
#update : if your record is already existing in your model:
your_record_id = qcontext.get('form_input_recordid')
your_model_record =
self.env['your_model_name'].browse(your_record_id)
your_model_record.write(values)
# OR create : if you want to create a new record in your model:
my_new_record = self.env['your_model_name'].create(values)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
