Skip to Content
Menu
This question has been flagged
1 Reply
4645 Views

model (relevant part):

class AcmeSapPricelist(models.Model):
    _name = "acme.sap.pricelist"
    _inherit = 'portal.mixin'
    _description = "Some Model"
    _order = 'date desc, id desc'
    partner_id = fields.Many2one('res.partner', string='Partner', required=True)
    company_id = fields.Many2one('res.company', string='Company', required=True, default=lambda self: self.env.user.company_id)
    name = fields.Char(string='Document name')
    origin = fields.Char(string='Document origin/source')
    date = fields.Datetime(string='Document creation date')
    note = fields.Char(string='Note')
    file = fields.Many2one(comodel_name="ir.attachment", string="SAP file")
    pricelist_lines = fields.One2many('acme.sap.pricelist.line', compute='_pricelist_lines', store=False)
    @api.multi
    def _pricelist_lines(self):
        self.pricelist_lines = request.env['acme.sap.pricelist.line'].search([('sap_pricelist_id', '=', 1)])


view (relevant part):

        <record id="view_acme_sap_pricelist_form" model="ir.ui.view">
            <field name="name">acme.sap.pricelist.form</field>
            <field name="model">acme.sap.pricelist</field>
            <field name="priority" eval="8" />
            <field name="arch" type="xml"> 
                <form string="Pricelist">
                    <sheet>
                        <div class="oe_title">
                            <h1>
                                <field name="name"/>
                            </h1>
                        </div>
                        <group>
                            <group>
                                <field name="date"/>
                                <field name="id"/>
                            </group>
                            <group>
                                <field name="partner_id"/>
                                <field name="csv"/>
                            </group>
                        </group>
                        <group>
                            <group>
                                <field name="origin"/>
                                <field name="note"/>
                            </group>
                        </group>
                    </sheet>
                </form>
            </field>
        </record>


 

Above (file = fields.Many2one(comodel_name="ir.attachment", string="SAP file")) is writing a record to ir.attachment table, however I'm unable to figure how to get the columns

res_model
res_model_name
res_field
res_id

filled too. All of them are set to NULL when uploading an file making it impossible to process the record further.


Based on some testing, at least res_model (=>res.partner in this case) and res_id (=>the partners Id) would be required (i.e. for allowing a website user to access the attachment via /web/content/id). How can I tell my module to pass on these two? 

Avatar
Discard
Author Best Answer

My approach now looks the following - seems like that's how you're supposed to do it:

model:

    @api.onchange('partner_id')
    def onchange_partner_id(self):
        if(self.csv):
            if(self.partner_id):
                self.csv.write({'res_model': 'res.partner', 'res_field': 'woa_sappricelist', 'res_id': self.partner_id})


view:

    <field attrs="{'invisible':[('partner_id', '==', False)]}" name="csv" context="{'default_res_model': 'res.partner', 'default_res_id': partner_id}"  />


attrs .. invisible is just do hide the field as long as no partner is selected, which just helps to make sure there is a partner selected before uploading a file.


Avatar
Discard
Related Posts Replies Views Activity
1
Jan 23
7734
2
Jul 20
2646
5
Dec 19
4319
2
Dec 23
11994
0
Oct 23
33