Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

2

Write method called in the create method

By
N
on 5/31/13, 1:13 PM 2,336 views

I have created a custom module and i am redefining the create and write methods. When i create a new sale order, the method create is called, but also the write method.. How it is possible ? Thanks

def create(self, cr, uid, vals, context=None):
    # creation du devis par heritage(et des lignes de commandes du devis) // obligatoire de creer le devis pour acceder aux lignes du devis
    id = super(sale_order, self).create(cr, uid, vals, context)
    # creation d'une liste vide qui contiendra tous les numeros de sections saisis dans la vue
liste_numeros_section = []
    # recuperation des id de chaque ligne du devis (qui ne sont pas des options)
    order_line_ids = self.pool.get('sale.order.line').search(cr, uid, [('order_id', '=', id), ('is_option', '=', False)], context=context)
    #recuperation de l'objet section
    section_pool = self.pool.get('sale.order.section') 
    for i in self.pool.get('sale.order.line').browse(cr,uid,order_line_ids, context=context): # pour chaque ligne du devis
        # si nouveau numero de section, alors creation section + lien avec ligne de devis actuelle
        if not(i.number_section in liste_numeros_section):
            liste_numeros_section.append(i.number_section)
            dict_section = {
                'order_id': id,
                'order_line': [(4, i.id)], # lier ligne du devis actuelle a la section (creation d'une relation)
                'number': i.number_section,
             }
             section_pool.create(cr,uid,dict_section, context=context)
        else:
            # si le numero de section existe deja, alors creation lien ligne du devis actuelle /section
            id_section = self.pool.get('sale.order.section').search(cr,uid, [('number', '=', i.number_section), ('order_id', '=', id)], context=context) #recupere section qui correspond a la ligne du devis
            dict_section = {
                'order_line': [(4, i.id)], # lier ligne du devis actuelle a la section (creation d'une relation)
            }
            section_pool.write(cr,uid,id_section,dict_section, context=context)
        return id

    def write(self, cr, user, ids, vals, context=None):
        raise osv.except_osv(_('Erreur!'),_('Vous ne pouvez pas confirmer une commande qui contient une ou des options'))
        # mise a jour du devis par heritage
        super(sale_order, self).write(cr, user, ids, vals, context)
        order_id = ids[0] # recuperation de l'id du devis courant
        # recuperation de l'objet ligne de devis
        order_line_pool = self.pool.get('sale.order.line')
        # recuperation de l'objet section
        section_pool = self.pool.get('sale.order.section')
# recuperation des id de chaque ligne du devis (qui ne sont pas des options)
        order_line_ids = order_line_pool.search(cr, user, [('order_id', '=', order_id), ('is_option', '=', False)], context=context)
        # recuperation des id de chaque section du devis
        section_ids = section_pool.search(cr, user, [('order_id', '=', order_id)], context=context)
# creation d'une liste vide qui contiendra tous les numeros de sections saisis dans la vue
            liste_numeros_section = []
        # pour chaque ligne du devis
        for i in self.pool.get('sale.order.line').browse(cr,user,order_line_ids, context=context):
            if not (i.section_id in section_ids): # si nouvelle section
                dict_section = {
                    'order_id': order_id,
    # lier ligne du devis actuelle a la section (creation d'une relation)
                    'order_line': [(4, i.id)],
                    'number': i.number_section,
                }
                section_pool.create(cr,user,dict_section, context=context)
            else:
                dict_section = {
                # mise a jour de la relation entre la section et la ligne du devis
                    'order_line': [(6, 0, i.id)],
                    'number': i.number_section,
                }
                section_pool.write(cr,user,i.section_id,dict_section, context=context)
        return True
0

patrick

--patrick--
1958
| 5 4 7
Baarn, Netherlands
--patrick--
patrick
On 5/31/13, 6:26 PM

In order to create a new record, you have to write it to the database. I think that the call to super(sale.order).create(..) will do a call to write (or self.write(..)). At the moment I do not have the code of openERP with me to check this.

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 5/31/13, 1:13 PM
Seen: 2336 times
Last updated: 3/27/15, 6:36 PM