0
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()

I'm Creating an indexing class that will index claim Topic field my problem that i m trying to add the record id of a claim after it s creation. More over my field ref which contains the reference is always missing in Postgres

Da3oub
  • 3
  • 4

1 Answers1

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'),
               }

You need a field in child class to hold the reference of parent class. Technically parent id will be stored in child, not child id in parent. Because we will have multiple child for single parent. In database this is purely foreign key. For your case,

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()
no coder
  • 2,290
  • 16
  • 18
  • Following your Parent / child example.All i need is the following : Child holds a fields.one2many() with each child having multiple reference towards Parents records . Now In the Parent i dont need any field holding reference towrds Child.It's not possible ? or when i create a one2many in child i need automatically to create a many2one in parent ? – Da3oub May 08 '15 at 14:33
  • Yes you need to create the many2one in the opposite `class` (table) of one2many. It is a must. It will be act as foreign key. Without it there will no relation between parent and child. Whether you need it or not, you need to put it. Because database needs it. – no coder May 08 '15 at 14:47
  • hummm, the funny thing been a while i was trying to do what i said following this Topic http://stackoverflow.com/questions/10600518/openerp-create-new-record-one2many-many2one-relationship i followed @Avadhesh method – Da3oub May 08 '15 at 15:00