Skip to Content
Menu
This question has been flagged

I want to add attachment while edit a form named my information on website and i want to save the attachment on the backend. I have added input field in the form for attachment but while saving the form i am not getting the attachment in the backend. Here is my code:-

template file:-

    <div class="form-group row">

     <div class="col-lg-3 col-md-4">
      <label class="control-label" for="Attachments">
       Attach Files
      </label>
     </div>
     <div class="col-lg-7 col-md-12">
       <input id='attachment' type="file"
       class="form-control o_website_form_input"
       name="attachment" multiple="true"
       data-show-preview="true"                                                                          
       accept="application/pdf"/>
      </div>
</div>


controller file:-


    @route(['/path/to/template'], type='http', auth='public', website=True)

def account(self, redirect=None, **post):
    values = self._prepare_portal_layout_values()
    User = request.env.user
    partner = User.partner_id
    
    if post:

        print("post::::::::::::::::::::::", post, values)
        attachment_ids = []
        attachment_list = request.httprequest.files.getlist('attachment')
        print("attachment_list::::::::::::::::", attachment_list)
        for att in attachment_list:
            if post.get('attachment'):
                attachments = {
                    'res_name': att.filename,
                    'res_model': 'res.partner',
                    'res_id': partner.sudo().id,
                    'datas': base64.encodestring(att.read()),
                    'type': 'binary',
                    'datas_fname': att.filename,
                    'name': att.filename,
                }
                attachment_obj = http.request.env['ir.attachment']
                att_record = attachment_obj.sudo().create(attachments)
                attachment_ids.append(att_record.id)
        if attachment_ids:
            values.update({'attachment_ids': [(6, 0, attachment_ids)]})

So Can anyone give me some idea what i am missing?

Avatar
Discard

Hi Gautam, did you solve your problem? i'm facing the same issue, so if you can help

Related Posts Replies Views Activity
2
Dec 19
2833
1
Nov 22
5755
4
Aug 20
8262
1
Dec 19
5114
2
Sep 24
118