← Back to team overview

openerp-community team mailing list archive

Re: [Openerp-expert-framework] openerp module development issue

 

On 05/24/2012 03:56 PM, ange désiré bouabré wrote:
> Excuse me everyone, I develop a module in openerp and I'm stuck in my work.I
> know I'm not allowed to send emails that way but I have no choice.I really need
> your help to continue. It's been a week since I did not advance. I just can not
> help me please:

You question is perfectly fine for the Community list or on the forum, or via
Launchpad answers, or on IRC (#openobject on Freenode), don't worry :-)
However please don't post such questions on the "openerp-expert-framework"
list, it is meant for advanced questions and framework discussions, not for
simple support.

Concerning your questions, see Niels' answer for an example of the code to
compute the sum. I have some extra minor remarks concerning what you already
wrote, see below, and an example function field that you could add to your
'client' model to show the sum on the Client form.


> I created 2 objects 'client.class' and 'vigne.class' with one2many and many2one
> fields because one client.class can get one or more vigne.class :
> 
> 
> *class client_class*(osv.osv) :
> 
> 
> _name = 'client.class'
                  ^^^^^
This will not cause problems, but using ".class" suffix for naming your models
is strange and useless. Usually they should be named "module.model", e.g.
"vineyard.vine".


> _inherit = 'res.partner'
> 
> _order ='nom'
> 
> _columns = {
> 
> *'client_id':fields.one2many('vigne.class','vigne_ids',"Vignes", ondelete='set
> null')*, #'vigne'

The names of your one2many field and its reverse many2one relationship are
swapped, which is *very* confusing. Here is how it should be, assuming your
module was named "vineyard":

class client(osv.osv):
    _name = 'vineyard.client'
    _inherit = 'res.partner'
    _columns = {
        'vine_ids': fields.one2many('vineyard.vine', 'client_id', 'Vines'),
    }

class vine(osv.osv):
    _name = 'vineyard.vine'
    _columns = {
        'client_id': fields.many2one('vineyard.client', 'Client'),
    }



> 'date':fields.datetime("Date de Creation"),

You might not need to have an explicit "date" field because all objects have an
implicit "create_date" field by default.


> 'nom': fields.char("Nom et Prenom",size=128),

No need for an extra "nom" field as you inherit from res.partner which has a
'name' field for that same purpose.



> I stocked value in fields *'duree_taillage' *and
> *'devis_taillage_total**'***of***vigne_class***and cause some
> ***client_class*have one or many *vigne_class*, i would like to *compute the
> sum***of these fields *'duree_taillage'***and *'**devis_taillage_total'***for
> each *client_class***objects created.
> 
> 
> ex : *client_class_object.**sum_duree_taillage =
> **vigne_class_1.**duree_taillage+ **vigne_class_2.**duree_taillage+
> **vigne_class_3.**duree_taillage*
> 
> and
> 
> *client_class_object.**sum_devis_taillage_total =
> ****vigne_class_1.**devis_taillage_total+
> **vigne_class_2.**devis_taillage_total+ **vigne_class_3.**devis_taillage_total*
> 
> 
> How browse the *vigne_class*objects of one *client_class* object and get these
> fields to compute them??

What you are looking for is a function field, that is a field that is
automatically computed by a python method when you read it.  Using your current
names, you could add a function field on the client.class to compute the sum of
'devis_taillage_total' like this:


class client_class(osv.osv):
    def _compute_taillage(self, cr, uid, ids, field, arg, context=None):
        result = {}
        for client in self.browse(cr, uid, ids, context=context):
            result[client.id] = sum(v.devis_taillage_total
                                        for v in client.client_id)
        return result

    _columns = {
        'devis_taillage_total': fields.function(_compute_taillage,
            type='float', string="Devis Taillage Total')
    }

And if you rename your fields properly as I suggested above, the only thing
that should change in this code is "for v in client.client_id" that will become
a more logical "for v in client.vine_ids"


References