通过python代码Odoo 12/python 3.7添加many2many和one2many记录

2024-06-07 00:24:39 发布

您现在位置:Python中文网/ 问答频道 /正文

几天以来,我一直在努力通过代码为许多领域增值,这让我陷入了困境。其想法是使用web服务从第三方网站获取一些数据,并在my odoo 12模块中重复使用这些数据。我能够解析JSON请求并使用model.Models创建实体。但是,对于使用one2many或many2many的字段,我无法添加它们的值并将它们链接到我的模型。这是我的python代码。我想要实现的是,在创建记录“book.db”之后,我想同时创建它的类别,并将其链接到当前记录。但是,每次我运行代码时,只创建“book.db”模型。类别模型从未创建或链接。如果可能的话,有人能给我指出正确的方向或更正我的代码吗。非常感谢

from odoo import models, fields, api
from . import prestashopproduct
import requests
import json


class Book(models.Model):
    _name = "book.db"

    prestashop_id = fields.Integer('Prestashop ID')
    title = fields.Char(string="book title")
    ean13_code = fields.Char(string="EAN13")
    author = fields.Char(string="book author")
    released = fields.Date(string="Date de publication")
    type = fields.Selection([('Numérique', 'Numérique'), ('Papier', 'Papier')], string="type")
    catalog = fields.Char(string="catalogue")
    collection = fields.Char(string="collection")
    isbn = fields.Char(string="Numero ISBN")
    description = fields.Html("Description")
    distributeur = fields.Char(string="Distribiteur")
    code_distribiture = fields.Char(string="Code distribiteur")
    code_collection = fields.Char(string="Code collection")
    code_dispo = fields.Char(string="Code dispo")
    code_tva1 = fields.Integer("Code tva1")
    code_tva2 = fields.Integer("Code tva2")
    presentation = fields.Html("Presentation")
    type_produit = fields.Char(string="Type de produit")
    theme_edilectre = fields.Char(string="Type de produit")
    categorie = fields.Html("categorie")
    poid = fields.Float("Poid en Gramme")
    prix = fields.Float("Prix")
    largeur = fields.Float("Largeur en MM")
    epaisseur = fields.Float("Epaisseur en MM")
    hauteur = fields.Float("Hauteur en MM")
    image = fields.One2many('product.images', 'product_id', string='Imags du produit')
    cate = fields.Many2many('product.cetegorie', 'product_id', string='Imags du produit')
    image_product = fields.Char("Image")

    @api.one
    def get_books(self):
        jso = prestashopproduct.Product.get_full_stock(self=prestashopproduct.Product())
        for j in jso['products']:
            if self.check_unicity(j['id']):
                book = [{'title': j['name'][1]['value'],
                         'ean13_code': j['ean13'],
                         'isbn': j['isbn'],
                         'epaisseur': j['width'],
                         'largeur': j['depth'],
                         'hauteur': j['height'],
                         'poid': j['weight'],
                         'prestashop_id': j['id'],
                         'description': j['description'][1]['value'],
                         'presentation': j['description_short'][1]['value'],
                         'categorie': j['description_short'][1]['value']}]
                record =  self.create(book)
                print (self.id)
                record.cate.create({'cate': [{'product_id': record.id, 'name': 'absc'}]})

    def check_unicity(self, id):
        if self.search_count([('prestashop_id', '=', id)]) > 0:
            return False
        else:
            return True


class Image(models.Model):
    _name = 'product.images'
    product_id = fields.Many2many('book.db', string='Prestashop ID')
    product_image = fields.Binary('Image du produit')
    product_image_url = fields.Char("product_image")

    def donload_product_image(self, product_id, image_id):
        image = prestashopproduct.Product.get_product_image(prestashopproduct.Product(), id_product=product_id,
                                                            id_image=image_id)
        return image


class Categories(models.Model):
    _name = 'product.cetegorie'
    product_id = fields.Many2many('book.db', string="Categories")
    nb_products_recursive = fields.Integer("nb_products_recursive")
    name = fields.Char("Descriptif")

    @api.one
    def new_record(self, product_id):
        self.create([{'product_id': product_id, 'name': 'a'}])

Tags: nameimageselfidfieldsdbstringcode
2条回答
    @api.model
    def _repare_cate_list(self, cates=[]):
        post_cates = []
        existing_add = []

        for cate_name in cates:
            cate_ids = self.env['product.cetegorie'].search([('name', '=', cate_name)])
            if cate_ids:
                existing_add.append(int(cate_ids[0]))
            else:
                post_cates.append((0, 0, {'name': cate_name}))

        post_cates.insert(0, [6, 0, existing_add])
        return post_cates

    @api.one
    def get_books(self):
        jso = prestashopproduct.Product.get_full_stock(self=prestashopproduct.Product())
        for j in jso['products']:
            if self.check_unicity(j['id']):
                cate_vals = self._repare_cate_list(['absc'])
                book = [{'title': j['name'][1]['value'],
                         'ean13_code': j['ean13'],
                         'isbn': j['isbn'],
                         'epaisseur': j['width'],
                         'largeur': j['depth'],
                         'hauteur': j['height'],
                         'poid': j['weight'],
                         'prestashop_id': j['id'],
                         'description': j['description'][1]['value'],
                         'presentation': j['description_short'][1]['value'],
                         'categorie': j['description_short'][1]['value'],
                         'cate':cate_vals
                         }]
                record =  self.create(book)

另外,从'product.cetegorie'模型中删除不需要的行product_id = fields.Many2many('book.db', string="Categories")。“多对多”使用单独的表链接类别以保存它们

每当您想要编辑、更新或删除One2manyMany2many字段时,请参阅以下几行

(0, 0, {values}) link to a new record that needs to be created with the given values ​​dictionary

(1, ID, {values}) update the linked record with id = ID (write values ​​on it)

(2, ID) remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)

(3, ID) cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself)

(4, ID) link to existing record with id = ID (adds a relationship)

(5) unlink all (like using (3, ID) for all linked records)

(6, 0, [IDs]) replace the list of linked IDs (like using (5) then (4, ID) for each ID in the list of IDs)

相关问题 更多 >