It is currently Mon May 21, 2012 6:49 pm

All times are UTC + 2 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: [SOLVED] How to show / use a field from a related object?
PostPosted: Mon May 30, 2011 2:54 pm 
Offline

Joined: Thu Mar 31, 2011 8:45 am
Posts: 241
Location: Málaga, Spain
Hi, i want to know if it is posible to show a field from an object called 'B' which is a many2one of object 'A'
Code:
class mod_a(osv.osv):
   _name = 'mod.a'
   _description = 'moda'
   _columns = {
      'txt':fields.char('type text here',size= 24)
                'dat':fields.char('datext', size=24)
mod_a()

class mod_b(osv.osv):
   _name = 'lopd.evaluacion'
   _description = 'Evaluación del fichero'
   _columns = {
      'related':fields.many2one('mod.a','Choose', required=True),
                'randomfield':fields.char('Abc', size="4")
mob_b()



I want to use a selection field on B form view, to select through A's but i want also to show some of the fields.

for example i want something like this in the object B form view
Code:
    <field name="related" widget="selection" />
    <field name="randomfield" />
    <field name="related.dat" readonly="True" />

_________________
Desarrollo Compuservice
Parque Tecnológico de Andalucía


Last edited by Reinhart on Wed Jun 01, 2011 9:27 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: How to show / use a field from a related object?
PostPosted: Mon May 30, 2011 3:11 pm 
Offline

Joined: Sun Dec 28, 2008 1:03 pm
Posts: 190
Location: Genoa
Hi,

did you see 'related' field? http://doc.openerp.com/v6.0/developer/2 ... onal-types

_________________
Lorenzo Battistini
http://planet.domsense.com/en/author/elbati/
http://www.agilebg.com/


Top
 Profile  
 
 Post subject: Re: How to show / use a field from a related object?
PostPosted: Tue May 31, 2011 9:26 am 
Offline

Joined: Thu Mar 31, 2011 8:45 am
Posts: 241
Location: Málaga, Spain
Ok, i see i tried this but it doesnt work, maybe i'm missing something?

This is the field i want to show in the object 'b' view, which is implemented in class 'a' called 'lopd.fichero':
Code:
   'nivel':fields.selection([('nivel1','Básico'),('nivel2','Medio'),('nivel3','Alto')],'Nivel', required= True),


And i tried this:
Code:
class
    'fichero':fields.many2one('lopd.fichero','Seleccione el fichero que se va a evaluar', required=True),
    'nivel':fields.related('nivel', 'nivel', type="selection", relation="lopd.fichero", string="Nivel de seguridad del fichero", store=False),



What am i doing wrong?

_________________
Desarrollo Compuservice
Parque Tecnológico de Andalucía


Top
 Profile  
 
 Post subject: Re: How to show / use a field from a related object?
PostPosted: Tue May 31, 2011 9:32 am 
Offline

Joined: Thu Sep 23, 2010 9:29 pm
Posts: 251
Location: Leeseringen, Germany
Reinhart wrote:
Code:
class
    'fichero':fields.many2one('lopd.fichero','Seleccione el fichero que se va a evaluar', required=True),
    'nivel':fields.related('nivel', 'nivel', type="selection", relation="lopd.fichero", string="Nivel de seguridad del fichero", store=False),

What am i doing wrong?


Your fields.related does try to traverse itself.

It's
Code:
'nivel':fields.related('fichero', 'nivel', type="selection", string="Nivel de seguridad del fichero", store=False),


relation= is only needed when the target type is a relation (many2one, many2many, one2many).

I don't know if type=selection works out of the box, or if you have to repeat the possible options somehow. Never tried that. I only used fields.related for showing additional readonly data, type=char works great there.

_________________
Light travels faster than sound.
That is why some people appear bright until you hear them speak.


Last edited by dieck on Tue May 31, 2011 9:55 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: How to show / use a field from a related object?
PostPosted: Tue May 31, 2011 9:53 am 
Offline

Joined: Thu Mar 31, 2011 8:45 am
Posts: 241
Location: Málaga, Spain
Hi, thanks for the reply, but doesnt work :(

I need 'nivel' field as a selection, could be a many2one but i think is excesive to create an entry in the database only to store that 3 values.

Is there any other way to represent that field in my view, i need it because depending on that field i must show different options.

_________________
Desarrollo Compuservice
Parque Tecnológico de Andalucía


Top
 Profile  
 
 Post subject: Re: How to show / use a field from a related object?
PostPosted: Wed Jun 01, 2011 9:27 am 
Offline

Joined: Thu Mar 31, 2011 8:45 am
Posts: 241
Location: Málaga, Spain
Finally I solved my problem by creating a function which is executed in the on_hange event of my many2one field 'fichero', and i stored the value on an invisible and non-storable field

Just if someone needs something similar here is the code i used:
Code:
def nivel_fichero (self, cr, uid, ids, dato):
      if (dato):
         cr.execute ('SELECT nivel FROM lopd_fichero WHERE id='+str(dato)+';')
         data = cr.fetchone()
         if data == None :
            return None
         desc = cr.description
   
         dict = {}
         for (name, value) in zip(desc, data) :
            dict[name[0]] = value
   
         v = { 'nivel':dict['nivel'],}
      else:
         v = { 'nivel':'0',}
      return { 'value':v }


I dunno if there is a simpliest solution in openerp for this, anyways this works for me.

_________________
Desarrollo Compuservice
Parque Tecnológico de Andalucía


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC + 2 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:

Protected by Anti-Spam ACP