在one2many关系中添加当前记录的引用

时间:2015-05-08 12:20:22

标签: python reference openerp-7

import openerp
from openerp import netsvc, tools, pooler
from openerp.osv import fields, osv
from openerp.tools.translate import _
import time
import sys
import os
import string
import logging

_logger = logging.getLogger(__name__)
#_logger.error("my variable : %r", my_var)



class reclamation(osv.Model):
    _name = '_reclamation'
    _columns = {
        'num': fields.char('Numéro'),
        'etat': fields.selection([('o','Ouvert'),('f','Fermé')],'Etat'),
        'catg': fields.selection([('p','Production'),('s','Sinistre')],'Catégorie'),
        'souscatg': fields.selection([('ppar','Production Particulier'),('ppro','Production Professionel'),('pent','Production Entreprise')],'Sous Catégorie'),
        'date' : fields.date('Date d\'ajout'),
        'sujet' : fields.text('Sujet'),
        'client': fields.many2one('res.partner','Client'),
    }
    _defaults = { 
        'num': lambda obj, cr, uid, context: obj.pool.get('ir.sequence').get(cr, uid, 'reg_code'), 
    }

    def create(self, cr, uid, values, context=None):
        _logger.error("Beast Mod Is on")
        new_id = super(reclamation, self).create(cr, uid, values, context=context)
        index_instance = self.pool.get('_index')
        txt_fil = values['sujet']
        #List of the stopwords 
        stopwords = ("For","This")
        #List of the delimiters
        delimiter_chars = ",.;:!?"
        #Split the Text into words delimited by whitespace.
        words = txt_fil.split()
        #Remove unwanted delimiter characters adjoining words.
        words1 = [ word.strip(delimiter_chars) for word in words ]
        #Remove stopwords.
        """"words2 = words1[:]
        for word in words1:
            if word in stopwords:
                words2.remove(word)"""
        words2 = []
        [words2.append(word) for word in words1 if word not in stopwords]
        #Remove Duplicate
        ulist = []
        [ulist.append(word) for word in words2 if word not in ulist]
        #Supposedly after this we open Index Table and store the words, their occurences , and a reference for the records in which they were found 
        for word in ulist:
            vals_index = {}
            vals_index['mot'] = word
            vals_index['ref'] = new_id
            vals_index['prio'] = 0
            index_instance.create(cr,uid, vals_index , context=context)
        return new_id

reclamation()    


class index(osv.Model):
    _name = '_index'
    _columns = {
        'mot': fields.char('Mot Clé'),
        'ref' : fields.one2many('_reclamation','num','Reclamation'),
        'prio': fields.integer('Priorité'),
    }
    _defaults = {
        'prio' : 0,
    }       
index()

我正在创建一个索引类,它会索引主题字段我的问题,我试图在创建后添加声明的记录ID。在Postgres中总是缺少包含引用的字段引用

1 个答案:

答案 0 :(得分:0)

class parent(osv.Model):
    _name = 'parent'
    _columns = {
               'field1' : fields.one2many('child','field2','Childs Field'),
               }

class child(osv.Model):
    _name = 'child'
    _columns = {
               'field2': fields.many2one('parent', 'Parents Field'),
               }

您需要子类中的字段来保存父类的引用。从技术上讲,父ID将存储在子级中,而不是父级中的子级ID。因为单亲会有多个孩子。在数据库中,这纯粹是外键。对于你的情况,

class reclamation(osv.Model):
    _name = '_reclamation'
    _columns = {
               'num': fields.many2one('_index', 'Numéro'),
               }
reclamation()  

class index(osv.Model):
    _name = '_index'
    _columns = {
               'ref' : fields.one2many('_reclamation','num','Reclamation'),
               }
index()