pythonormodooodoo-10

Odoo10 - Inherit Model to TransientModels with Many2Many Fields


I have an Odoo model named SapPlOutstandingComment, which includes many-to-many and one-to-many fields as shown below:

class SapPlOutstandingComment(models.Model):
    _name = "sap.pl.outstanding.comment"
    _description = "PL Manual Outstanding Comment"
    _rec_name = "plant_id"

    plant_id = fields.Many2one('sap.mm.plant',string='Department', ondelete='restrict')
    period_id = fields.Many2one('account.period', string='Month', ondelete='restrict')
    description = fields.Many2one('sap.pl.id.mapping',string='Description', ondelete='restrict')
    category = fields.Many2one('sap.pl.comment.category',string='Category', ondelete='restrict')
    currency_id = fields.Many2one('res.currency', string='Currency', default=lambda self: self.env.user.company_id.currency_id)
    amount = fields.Monetary(string='Amount')
    action = fields.Selection([('take out/add in', 'Take Out/Add In'), ('move', 'Move')], string="Type of Comments")
    reconcile = fields.Boolean(string="Reconcile", default=False)
    reconcile_comment = fields.Many2many('sap.pl.outstanding.comment','sn_sap_pnl_oc_reconcile_rel', 'sap_pl_outstanding_comment_id','reconcile_comment_id', string='Outstanding Comment')
    move_to = fields.Many2one('sap.pl.id.mapping', string='Move To', ondelete='restrict')
    reason = fields.Text(string="Reason")
    status = fields.Selection([('open', 'Open'), ('closed', 'Closed')], string="Status", default="open")
    close_comment = fields.Boolean(default=False)
    can_select_action = fields.Boolean(default=False)
    can_close_comment = fields.Boolean(default=False)

Now, I want to allow users to input data through a Wizard with the same conditions and validations as the original model. While I'm aware that I could create a new transient model and replicate the code, maintaining it would become challenging, especially if there are changes in the validation rules of the master tables.

My question is, can I use inheritance to achieve this? Essentially, I want to include all the model fields in the transient model.

Any advice or suggestions would be greatly appreciated. Thank you in advance!


Solution

  • You can manage it using a many2one field ("sap_model") on your original model ("sap.pl.outstanding.comment") and related fields for each needed fields of this original model:

    class SapPlOutstandingWizard(models.TransientModel):
        _name = 'sap.pl.outstanding.wizard'
         
         # Many2One field to link to your basis model:
         sap_model= fields.Many2one('sap.pl.outstanding.comment', string="basis model")
         
         # Related fields, that are associated with the needed fields of your basis model:
         rel_plant_id = fields.Many2one(related='sap_model.plant_id',string='Department')
         rel_period_id = fields.Many2one('sap_model.period_id', string='Month')
         #...
         #...