Skip to Content
Menu
This question has been flagged
1 Reply
3789 Views

Hello,

I want to create some records in a table when I create a record in another table with Many2one fields.

I explain me :

I've two classes :

- miadi_packaging :

import odoo from odoo import models, fields, tools, api

class miadi_packaging(models.Model):

    _name = 'miadi.packaging'

    _rec_name = 'conditionnement_id'

    _order = 'conditionnement_id asc'

    conditionnement_id = fields.Char(string='Code', size=4, default='', required=True)

    conditionnement_nom = fields.Char(string='Packaging Name', default='', size=128, required=True)

    nb_articles = fields.Integer(string='Number of products', default=0, required=True)

     _sql_constraints = [

    ('uniq_id', 'unique(conditionnement_id)', 'A packaging already exists with this ID. It must be unique !'),

    ('uniq_name', 'unique(conditionnement_nom)', 'A packaging already exists with this name. It must be unique !'),

    ]

    @api.model

    @api.multi

    def create(self, vals):

            new_ratio = super(miadi_packaging, self).create(vals)

            productRatio = self.env['miadi.poidsconditionnement']

            productVariants = self.env['product.product']

            products = productVariants.search([])

            for rec in products:

                    nom_produit = rec.display_name

                    if nom_produit:

                            Ratio = productRatio.create({'conditionnement_id': new_ratio.conditionnement_id, 'produit_id': nom_produit})

            return new_ratio

"""when I create a record in this table, It creates some records in another table for each product"""


- miadi_poidsConditionnement :

class miadi_poidsConditionnement(models.Model):

    _name = 'miadi.poidsconditionnement'

    conditionnement_id = fields.Many2one('miadi.packaging', 'Packaging', default='') 

    produit_id = fields.Many2one('product.product', 'Product', default='') 

    nb_articles = fields.Integer(string='Number of products', default=0)

    poids = fields.Float(string='Packaging Weight', default=0)

    _sql_constraints = [

    ('uniq_id', 'unique(produit_id, conditionnement_id)', 'A product already exists with this packaging !'),

    ]


"""It's in this table that the records are created but it gives me an error because the "conditionnement_id" field and the "produit_id" field are many2one. The error is DataError: ERREUR: syntaxe en entrée invalide pour l'entier : « 2DZ »

LINE 1: ...(nextval('miadi_poidsconditionnement_id_seq'), 0, '2DZ', 39,...


It's saying that there is a syntax error for the integer "2DZ" because it expects an integer because the field is a many2one.


How can I do it ?



PS : It works if I put the "conditionnement_id" and "produit_id" in Char instead of Many2one but I want a many2one because if I want to create manually a record in this table, I want the list of products and the list of packagings.

I think I need the ID of the packaging and the id of the product BUT when I list all of my records (in miadi.poidsconditionnement), I want that it's written the name of products and packagings but not their ID.


I'm on Odoo 10


Thanks

Avatar
Discard
Author Best Answer

Hi,

I found the solution !!

I have to replace "nom_produit = rec.display_name" by "nom_produit = rec.id" and "new_ratio.conditionnement_id" by "new_ratio.id"

Avatar
Discard