Hello @All,
need help to how to implement section in my custom module.
after adding section and note can't do any action more. Can't save the form view.
here is my code:
Model:
class SodistraAttachementPrestataireLine(models.Model):
_name = "attachement_prestataire.principale.line"
name = fields.Char("Description")
product_id = fields.Many2one("product.product", "Désignation")
product_uom = fields.Many2one('uom.uom', string='Unité')
quantity = fields.Float("Qté")
quantity_consomme = fields.Float("Qté consommée")
price_unit = fields.Float("Prix unitaire")
montant = fields.Monetary("Montant", store=True, compute="_get_montant")
currency_id = fields.Many2one('res.currency', store=True, string='Currency',
default=lambda self: self.env.user.company_id.currency_id.id)
attachement_prestataire_id = fields.Many2one("attachement_prestataire.principale")
sequence = fields.Integer(default=10)
display_type = fields.Selection([
('line_section', 'Section'),
('line_note', 'Note'),
], default=False, help="Technical field for UX purpose.")
@api.depends('product_id')
@api.onchange('product_id')
def _get_name(self):
for line in self:
line.name = line.product_id.name
@api.depends('quantity', 'price_unit')
@api.onchange('quantity', 'price_unit')
def _get_montant(self):
for line in self:
line.montant = line.price_unit * line.quantity
@api.model_create_multi
def create(self, vals_list):
print(vals_list)
for vals in vals_list:
if vals.get('display_type', self.default_get(['display_type'])['display_type']):
vals.update(product_id=False, quantity=0, product_uom=False)
return super().create(vals_list)
def write(self, values):
if 'display_type' in values and self.filtered(lambda line: line.display_type != values.get('display_type')):
raise UserError(_("You cannot change the type of a sale quote line. Instead you should delete the current line and create a new line of the proper type."))
return super().write(values)
View:
<field name="line_ids" widget="section_and_note_one2many">
<tree editable="bottom" string="Ligne attachement" context="{'default_display_type': False}" default_order="sequence, id">
<control>
<create name="add_line_control" string="Ajouter une ligne"/>
<create name="add_section_control" string="Add une section" context="{'default_display_type': 'line_section'}"/>
<create name="add_note_control" string="Add a note" context="{'default_display_type': 'line_note'}"/>
</control>
<field name="sequence" widget="handle"/>
<field name="product_id" required="1"/>
<field name="display_type" force_save="1" invisible="1"/>
<field name="name" widget="section_and_note_text"/>
<field name="product_uom"/>
<field name="quantity"/>
<field name="quantity_consomme" readonly="1"/>
<field name="price_unit"/>
<field name="montant"/>
<field name="attachement_prestataire_id" invisible="1"/>
</tree>
</field>