It is currently Mon May 21, 2012 7:23 pm

All times are UTC + 2 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: howto setup a one2many to ir.attachment
PostPosted: Sun Apr 04, 2010 5:14 pm 
Offline

Joined: Fri Feb 19, 2010 10:15 am
Posts: 16
Location: Utrecht NL
I've been trying to find a way to show and manage attachments inside the form, rather than by using the 'attachment' menu button. The main reason is that I need to be able to manage attachments in sub-windows (without the menu buttons).

I've tried using fields.one2many but I keep getting exceptions.

I can also use fields.function to recreate the functionality, but that seems like re-inventing the wheel.

Any ideas anyone?

thanks.
Paul.

_________________
Paul Stevens paul at nfg.nl
NFG NET FACILITIES GROUP


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 08, 2010 3:03 pm 
Offline

Joined: Wed Mar 18, 2009 7:39 pm
Posts: 128
Location: Gatineau QC, Canada
Hi pjstevns,

There shouldn't be a problem with o2m fields involving attachments. Can you post the relevant parts of your code?

(Also, note that there is already a menu action leading to the Attachment forms. It's located in 'Administration / Low Level Objects'.)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 08, 2010 4:26 pm 
Offline

Joined: Fri Feb 19, 2010 10:15 am
Posts: 16
Location: Utrecht NL
Thanks for replying.

I've been trying with code like this:

Code:
        'file_ids': fields.one2many(
            'ir.attachment',
            'res_id',
            'Attachments',
            domain=[('res_model','=','realestate.property')],
        ),


which doesn't work :-( Wrong approach I assume.

_________________
Paul Stevens paul at nfg.nl
NFG NET FACILITIES GROUP


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 08, 2010 10:36 pm 
Offline

Joined: Wed Mar 18, 2009 7:39 pm
Posts: 128
Location: Gatineau QC, Canada
The approach should work. Try putting domain restrictions in quotes, like this:
Code:
'file_ids': fields.one2many(
    'ir.attachment',
    'res_id',
    'Attachments',
    domain="[('res_model','=','realestate.property')]",
),

This should at least compile but may actually be insufficient to make the whole thing work. Did you add any XML to alter the view? Regarding the whole approach, did you hack into the distribution source code or did you derived a new model using OpenObject inheritance constructs?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 09, 2010 10:05 am 
Offline

Joined: Fri Feb 19, 2010 10:15 am
Posts: 16
Location: Utrecht NL
I've constructed a custom object type that inherits directly from osv.osv. I need to be able to show attachments inside a form, because this class is only accessible from a sub-window.

I'm building a customization of project.project that refers to these realestate.property objects in a m2m relation. From the m2m widget you click on a realestate property that opens in a sub-window. Attachments are linked to these realestate properties, but the attachments button is not visible in these sub-windows - alas. If there's another module that does this, I can't find it despite extensive grepping of the code.

I started out with the quotes but noticed by scanning the openobject trunk source that other modules don't use quotes to pass a domain keyword argument to fields.many2many. I'm not sure it makes a difference.

_________________
Paul Stevens paul at nfg.nl
NFG NET FACILITIES GROUP


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 12, 2010 4:14 pm 
Offline

Joined: Wed Mar 18, 2009 7:39 pm
Posts: 128
Location: Gatineau QC, Canada
Hi again pjstevns,

Quote:
I've tried using fields.one2many but I keep getting exceptions.

What exceptions do you get? By which component is it thrown (server, GTK client, Web client, Python interpreter)? Can you post all of your Python class for the inherited model? Also, I'm not sure why you first talked about a o2m relation and then switched to a m2m relation?

If you just want to add one field to an existing model, for such a simple customisation you may want to consider doing it directly from within the interface instead of developing a whole new module. If you only have a bunch a simple customisations, you can record and export them automatically in a module by installing the 'base_module_record' module.


Top
 Profile  
 
 Post subject: Re: howto setup a one2many to ir.attachment
PostPosted: Wed Jul 20, 2011 2:16 pm 
Offline

Joined: Thu Oct 28, 2010 1:51 pm
Posts: 53
Incase anyone is looking into this i got the same exceptions as well, coming from base.calendar function base_calendar_id2real_id
It looks like this line:
return base_calendar_id and int(base_calendar_id) or base_calendar_id
is breaking with the int part, basically the one2many relation is sending a [id] which isnt compatible with the int part of the expression. (Its saying it cant int a list object)
I managed to patch this by basically enclosing it in a try block:
try:
return base_calendar_id and int(base_calendar_id) or base_calendar_id
except Exception,e:
return base_calendar_id and base_calendar_id or base_calendar_id

Now the relation is working fine.
If anyone knows how to override that global function in base_calendar.py without having to modify that source file it would be useful,

Best Regards,
Mike

_________________
Developing OpenERP Modules for what seems like an eternity
Smart IT Ltd
http://www.smart-ltd.co.uk


Top
 Profile  
 
 Post subject: Re: howto setup a one2many to ir.attachment ->SOLVED
PostPosted: Thu Jul 21, 2011 12:59 pm 
Offline

Joined: Thu Oct 28, 2010 1:51 pm
Posts: 53
Further note, this only works if you want to look at attachments.
It doesnt seem to pass the res model through at all if your creating so:
To 'create' attachments from that view is a right pain, it wont seem to let me write to res_model in the default get so... Pass the context with the model through in the one to many xml (just put it as a string)
Then I created a new document class overridden from:
class document_file(osv.osv):
_inherit = 'ir.attachment'
def default_get(self, cr, user,fields_list=[], context=None):
#put code to check context is there etc
data['tmp_res_model']=str(context['res_model'])
return data
_columns = {
'tmp_res_model':fields.char('Res Model',size=256),
}

create an override view (Not sure if this part is necessary):
<record id="ir_attachment_model_inherit" model="ir.ui.view">
<field name="name">ir.attachment.model.form.inherit</field>
<field name="model">ir.attachment</field>
<field name="inherit_id" ref="document.view_document_file_form"/>
<field name="arch" type="xml">
<field name="user_id" position="after">
<field name="tmp_res_model" />
<newline/>
</field>
</field>
</record>

Then copy the create out of the document.py code...
def create(self, cr, uid, vals, context=None):
#all code here from document.py
then at end before cr.commit()
if(vals and 'tmp_res_model' in vals):
self.pool.get('ir.attachment').browse(cr,uid,result).write({'res_model':vals['tmp_res_model']})

NOW it will actually 'SAVE' documents using this view.

_________________
Developing OpenERP Modules for what seems like an eternity
Smart IT Ltd
http://www.smart-ltd.co.uk


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 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