ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
How to attach file using upload button
Odoo 18
Enterprise
Enterprise
Introduction
In this slide, we’ll discuss on how to attach file using upload
button Odoo 18. Odoo features a dedicated model,
'ir.attachments,' designed for storing attachments submitted by
end users. We can see the process of utilizing the
'ir.attachments' model to enable file uploads through web forms
in this slide.
Enterprise
We need to create a field in our model to handle attachments. So, let's
create a many2many field in the res.partner model for storing
attachments.
file_attachment_ids = fields.Many2many('ir.attachment',
string='Attachments')
To set up the web form, we need to create an XML file. In this file,
we use the 'form' tag to define the different fields we want,
including the one for attaching files. The 'form' tag comes with
various attributes like 'class,' 'action,' 'method,' and 'enctype.'Now,
the 'enctype' attribute is particularly important when dealing with
attachments. It tells the form how to handle the characters in the
file. In simple terms, it ensures that the file content is not altered or
encoded in a way that could cause issues when submitting it. Think
of it as a special instruction to handle files properly.
Enterprise
Here's an example 'form' tag with the 'enctype' attribute:
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<template id="website_partner_form">
<t t-call="website.layout">
<div id="wrap" class="oe_structure oe_empty">
<section class="s_website_form" data-
vcss="001" data-snippet="s_website_form">
<div class="container">
<form action="/form/submit" method="post"
enctype="multipart/form-data">
<input type="hidden" name="csrf_token" t-att-
value="request.csrf_token()"/>
<div class="col-lg-7 col-md-8">
<label class="col-md-3 col-sm-4
control-label" for="att">Attach file</label>
</div>
Enterprise
<div class="col-lg-7 col-md-8">
<input type="file"
name="att" accept="image/*,application/pdf,video/*"/>
</div>
<button type="submit"
class="btn btn-primary">Create </button>
</form>
</div>
</section>
</div>
</t>
</template>
</odoo>
Enterprise
In the provided example, The 'action' attribute ("/form/submit")
specifies where the form data will be sent, while the 'method' attribute
("post") ensures that this data is not visible in the URL for security
reasons. The 'enctype' attribute ("multipart/form-data") is particularly
crucial for handling attachments, ensuring that the file content
remains intact during the submission process. Notably, the 'csrf_token'
serves as a security measure to guard against Cross-Site Request
Forgery (CSRF) attacks. Within the form, a user-friendly section for
attaching files is created using the 'label' and 'input' elements. The
'name' attribute ("att") is employed to identify the attached data, and
the 'accept' attribute restricts file types to images, PDFs, and videos.
Finally, the 'button' element initiates the form submission,
commencing the process of creating and saving the submitted data.
Enterprise
This will create a file input field like this. We can add the file by
clicking on choose file button.
Enterprise
For attaching files to partners, simply select the desired file and click
"Create." This action will add the file as an attachment to the
corresponding field in the partner record.
Enterprise
In the controller, we need to create a function that will handle
encoding the file and then saving it to the database. Below is a
sample code:
import base64
from odoo import http
from odoo.http import route, request, Controller
class FileUpload(http.Controller):
@http.route('/partner/form', type='http',
auth='public', website=True)
def file_upload(self, redirect=None, **kw):
current_partner_id = request.env.user.partner_id
file_name = kw.get('att').filename
file = kw.get('att')
Enterprise
attachment_id =
request.env['ir.attachment'].create({
'name': file_name,
'type': 'binary',
'datas': base64.b64encode(file.read()),
'res_model': current_partner_id._name,
'res_id': current_partner_id.id
})
current_partner_id.update({
'file_attachment_ids': [(4, attachment_id.id)],
})
return
request.render('custom_,module.website_partner_form')
Enterprise
In the partner record, we can see the attachment added in the
many2many field.
For More Info.
Check our company website for related blogs
and Odoo book.
Check our YouTube channel for
functional and technical videos in Odoo.
Enterprise
www.cybrosys.com

More Related Content

More from Celine George (20)

How to Configure Flexible Working Schedule in Odoo 18 Employee
How to Configure Flexible Working Schedule in Odoo 18 EmployeeHow to Configure Flexible Working Schedule in Odoo 18 Employee
How to Configure Flexible Working Schedule in Odoo 18 Employee
Celine George
Ìý
Managing expiration dates of products in odoo
Managing expiration dates of products in odooManaging expiration dates of products in odoo
Managing expiration dates of products in odoo
Celine George
Ìý
How to Unblock Payment in Odoo 18 Accounting
How to Unblock Payment in Odoo 18 AccountingHow to Unblock Payment in Odoo 18 Accounting
How to Unblock Payment in Odoo 18 Accounting
Celine George
Ìý
How to Configure Proforma Invoice in Odoo 18 Sales
How to Configure Proforma Invoice in Odoo 18 SalesHow to Configure Proforma Invoice in Odoo 18 Sales
How to Configure Proforma Invoice in Odoo 18 Sales
Celine George
Ìý
How to Configure Deliver Content by Email in Odoo 18 Sales
How to Configure Deliver Content by Email in Odoo 18 SalesHow to Configure Deliver Content by Email in Odoo 18 Sales
How to Configure Deliver Content by Email in Odoo 18 Sales
Celine George
Ìý
Effective Product Variant Management in Odoo 18
Effective Product Variant Management in Odoo 18Effective Product Variant Management in Odoo 18
Effective Product Variant Management in Odoo 18
Celine George
Ìý
How to create security group category in Odoo 17
How to create security group category in Odoo 17How to create security group category in Odoo 17
How to create security group category in Odoo 17
Celine George
Ìý
Odoo 18 Accounting Access Rights - Odoo 18 ºÝºÝߣs
Odoo 18 Accounting Access Rights - Odoo 18 ºÝºÝߣsOdoo 18 Accounting Access Rights - Odoo 18 ºÝºÝߣs
Odoo 18 Accounting Access Rights - Odoo 18 ºÝºÝߣs
Celine George
Ìý
Inventory Reporting in Odoo 17 - Odoo 17 Inventory App
Inventory Reporting in Odoo 17 -  Odoo 17 Inventory AppInventory Reporting in Odoo 17 -  Odoo 17 Inventory App
Inventory Reporting in Odoo 17 - Odoo 17 Inventory App
Celine George
Ìý
How to Configure Recurring Revenue in Odoo 17 CRM
How to Configure Recurring Revenue in Odoo 17 CRMHow to Configure Recurring Revenue in Odoo 17 CRM
How to Configure Recurring Revenue in Odoo 17 CRM
Celine George
Ìý
One Click RFQ Cancellation in Odoo 18 - Odoo ºÝºÝߣs
One Click RFQ Cancellation in Odoo 18 - Odoo ºÝºÝߣsOne Click RFQ Cancellation in Odoo 18 - Odoo ºÝºÝߣs
One Click RFQ Cancellation in Odoo 18 - Odoo ºÝºÝߣs
Celine George
Ìý
How to Create Meetings & Channels in Odoo 18 Discuss
How to Create Meetings & Channels in Odoo 18 DiscussHow to Create Meetings & Channels in Odoo 18 Discuss
How to Create Meetings & Channels in Odoo 18 Discuss
Celine George
Ìý
How to Configure Canned Response & Chatbots in Odoo 18
How to Configure Canned Response & Chatbots in Odoo 18How to Configure Canned Response & Chatbots in Odoo 18
How to Configure Canned Response & Chatbots in Odoo 18
Celine George
Ìý
How to Manage Restricted Category in Odoo 18 POS
How to Manage Restricted Category in Odoo 18 POSHow to Manage Restricted Category in Odoo 18 POS
How to Manage Restricted Category in Odoo 18 POS
Celine George
Ìý
How to Split Bills in Odoo 18 POS - Odoo 18
How to Split Bills in Odoo 18 POS - Odoo 18How to Split Bills in Odoo 18 POS - Odoo 18
How to Split Bills in Odoo 18 POS - Odoo 18
Celine George
Ìý
How to Change the Color on a Progress Bar in odoo 18
How to Change the Color on a Progress Bar in odoo 18How to Change the Color on a Progress Bar in odoo 18
How to Change the Color on a Progress Bar in odoo 18
Celine George
Ìý
How to Create Owl mixin in Odoo 17 - Odoo 17 ºÝºÝߣs
How to Create Owl mixin in Odoo 17 - Odoo 17 ºÝºÝߣsHow to Create Owl mixin in Odoo 17 - Odoo 17 ºÝºÝߣs
How to Create Owl mixin in Odoo 17 - Odoo 17 ºÝºÝߣs
Celine George
Ìý
What is Mixin Class & How to Use Mixin Classes in Odoo 18
What is Mixin Class & How to Use Mixin Classes in Odoo 18What is Mixin Class & How to Use Mixin Classes in Odoo 18
What is Mixin Class & How to Use Mixin Classes in Odoo 18
Celine George
Ìý
How to Define Menu & Actions in Odoo 18 - Odoo 18 ºÝºÝߣs
How to Define Menu & Actions in Odoo 18 - Odoo 18 ºÝºÝߣsHow to Define Menu & Actions in Odoo 18 - Odoo 18 ºÝºÝߣs
How to Define Menu & Actions in Odoo 18 - Odoo 18 ºÝºÝߣs
Celine George
Ìý
How to use search_count() method in Odoo 18
How to use search_count() method in Odoo 18How to use search_count() method in Odoo 18
How to use search_count() method in Odoo 18
Celine George
Ìý
How to Configure Flexible Working Schedule in Odoo 18 Employee
How to Configure Flexible Working Schedule in Odoo 18 EmployeeHow to Configure Flexible Working Schedule in Odoo 18 Employee
How to Configure Flexible Working Schedule in Odoo 18 Employee
Celine George
Ìý
Managing expiration dates of products in odoo
Managing expiration dates of products in odooManaging expiration dates of products in odoo
Managing expiration dates of products in odoo
Celine George
Ìý
How to Unblock Payment in Odoo 18 Accounting
How to Unblock Payment in Odoo 18 AccountingHow to Unblock Payment in Odoo 18 Accounting
How to Unblock Payment in Odoo 18 Accounting
Celine George
Ìý
How to Configure Proforma Invoice in Odoo 18 Sales
How to Configure Proforma Invoice in Odoo 18 SalesHow to Configure Proforma Invoice in Odoo 18 Sales
How to Configure Proforma Invoice in Odoo 18 Sales
Celine George
Ìý
How to Configure Deliver Content by Email in Odoo 18 Sales
How to Configure Deliver Content by Email in Odoo 18 SalesHow to Configure Deliver Content by Email in Odoo 18 Sales
How to Configure Deliver Content by Email in Odoo 18 Sales
Celine George
Ìý
Effective Product Variant Management in Odoo 18
Effective Product Variant Management in Odoo 18Effective Product Variant Management in Odoo 18
Effective Product Variant Management in Odoo 18
Celine George
Ìý
How to create security group category in Odoo 17
How to create security group category in Odoo 17How to create security group category in Odoo 17
How to create security group category in Odoo 17
Celine George
Ìý
Odoo 18 Accounting Access Rights - Odoo 18 ºÝºÝߣs
Odoo 18 Accounting Access Rights - Odoo 18 ºÝºÝߣsOdoo 18 Accounting Access Rights - Odoo 18 ºÝºÝߣs
Odoo 18 Accounting Access Rights - Odoo 18 ºÝºÝߣs
Celine George
Ìý
Inventory Reporting in Odoo 17 - Odoo 17 Inventory App
Inventory Reporting in Odoo 17 -  Odoo 17 Inventory AppInventory Reporting in Odoo 17 -  Odoo 17 Inventory App
Inventory Reporting in Odoo 17 - Odoo 17 Inventory App
Celine George
Ìý
How to Configure Recurring Revenue in Odoo 17 CRM
How to Configure Recurring Revenue in Odoo 17 CRMHow to Configure Recurring Revenue in Odoo 17 CRM
How to Configure Recurring Revenue in Odoo 17 CRM
Celine George
Ìý
One Click RFQ Cancellation in Odoo 18 - Odoo ºÝºÝߣs
One Click RFQ Cancellation in Odoo 18 - Odoo ºÝºÝߣsOne Click RFQ Cancellation in Odoo 18 - Odoo ºÝºÝߣs
One Click RFQ Cancellation in Odoo 18 - Odoo ºÝºÝߣs
Celine George
Ìý
How to Create Meetings & Channels in Odoo 18 Discuss
How to Create Meetings & Channels in Odoo 18 DiscussHow to Create Meetings & Channels in Odoo 18 Discuss
How to Create Meetings & Channels in Odoo 18 Discuss
Celine George
Ìý
How to Configure Canned Response & Chatbots in Odoo 18
How to Configure Canned Response & Chatbots in Odoo 18How to Configure Canned Response & Chatbots in Odoo 18
How to Configure Canned Response & Chatbots in Odoo 18
Celine George
Ìý
How to Manage Restricted Category in Odoo 18 POS
How to Manage Restricted Category in Odoo 18 POSHow to Manage Restricted Category in Odoo 18 POS
How to Manage Restricted Category in Odoo 18 POS
Celine George
Ìý
How to Split Bills in Odoo 18 POS - Odoo 18
How to Split Bills in Odoo 18 POS - Odoo 18How to Split Bills in Odoo 18 POS - Odoo 18
How to Split Bills in Odoo 18 POS - Odoo 18
Celine George
Ìý
How to Change the Color on a Progress Bar in odoo 18
How to Change the Color on a Progress Bar in odoo 18How to Change the Color on a Progress Bar in odoo 18
How to Change the Color on a Progress Bar in odoo 18
Celine George
Ìý
How to Create Owl mixin in Odoo 17 - Odoo 17 ºÝºÝߣs
How to Create Owl mixin in Odoo 17 - Odoo 17 ºÝºÝߣsHow to Create Owl mixin in Odoo 17 - Odoo 17 ºÝºÝߣs
How to Create Owl mixin in Odoo 17 - Odoo 17 ºÝºÝߣs
Celine George
Ìý
What is Mixin Class & How to Use Mixin Classes in Odoo 18
What is Mixin Class & How to Use Mixin Classes in Odoo 18What is Mixin Class & How to Use Mixin Classes in Odoo 18
What is Mixin Class & How to Use Mixin Classes in Odoo 18
Celine George
Ìý
How to Define Menu & Actions in Odoo 18 - Odoo 18 ºÝºÝߣs
How to Define Menu & Actions in Odoo 18 - Odoo 18 ºÝºÝߣsHow to Define Menu & Actions in Odoo 18 - Odoo 18 ºÝºÝߣs
How to Define Menu & Actions in Odoo 18 - Odoo 18 ºÝºÝߣs
Celine George
Ìý
How to use search_count() method in Odoo 18
How to use search_count() method in Odoo 18How to use search_count() method in Odoo 18
How to use search_count() method in Odoo 18
Celine George
Ìý

Recently uploaded (20)

The Broccoli Dog's inner voice (look A)
The Broccoli Dog's inner voice  (look A)The Broccoli Dog's inner voice  (look A)
The Broccoli Dog's inner voice (look A)
merasan
Ìý
Information Technology for class X CBSE skill Subject
Information Technology for class X CBSE skill SubjectInformation Technology for class X CBSE skill Subject
Information Technology for class X CBSE skill Subject
VEENAKSHI PATHAK
Ìý
Principle and Practices of Animal Breeding || Boby Basnet
Principle and Practices of Animal Breeding || Boby BasnetPrinciple and Practices of Animal Breeding || Boby Basnet
Principle and Practices of Animal Breeding || Boby Basnet
Boby Basnet
Ìý
Digital Tools with AI for e-Content Development.pptx
Digital Tools with AI for e-Content Development.pptxDigital Tools with AI for e-Content Development.pptx
Digital Tools with AI for e-Content Development.pptx
Dr. Sarita Anand
Ìý
Essentials of a Good PMO, presented by Aalok Sonawala
Essentials of a Good PMO, presented by Aalok SonawalaEssentials of a Good PMO, presented by Aalok Sonawala
Essentials of a Good PMO, presented by Aalok Sonawala
Association for Project Management
Ìý
Lesson Plan M1 2024 Lesson Plan M1 2024 Lesson Plan M1 2024 Lesson Plan M1...
Lesson Plan M1 2024  Lesson Plan M1 2024  Lesson Plan M1 2024  Lesson Plan M1...Lesson Plan M1 2024  Lesson Plan M1 2024  Lesson Plan M1 2024  Lesson Plan M1...
Lesson Plan M1 2024 Lesson Plan M1 2024 Lesson Plan M1 2024 Lesson Plan M1...
pinkdvil200
Ìý
Research & Research Methods: Basic Concepts and Types.pptx
Research & Research Methods: Basic Concepts and Types.pptxResearch & Research Methods: Basic Concepts and Types.pptx
Research & Research Methods: Basic Concepts and Types.pptx
Dr. Sarita Anand
Ìý
A PPT Presentation on The Princess and the God: A tale of ancient India by A...
A PPT Presentation on The Princess and the God: A tale of ancient India  by A...A PPT Presentation on The Princess and the God: A tale of ancient India  by A...
A PPT Presentation on The Princess and the God: A tale of ancient India by A...
Beena E S
Ìý
TRANSFER OF PATIENTS IN HOSPITAL SETTING.pptx
TRANSFER OF PATIENTS IN HOSPITAL SETTING.pptxTRANSFER OF PATIENTS IN HOSPITAL SETTING.pptx
TRANSFER OF PATIENTS IN HOSPITAL SETTING.pptx
PoojaSen20
Ìý
TLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptx
TLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptxTLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptx
TLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptx
RizaBedayo
Ìý
APM People Interest Network Conference - Tim Lyons - The neurological levels ...
APM People Interest Network Conference - Tim Lyons - The neurological levels ...APM People Interest Network Conference - Tim Lyons - The neurological levels ...
APM People Interest Network Conference - Tim Lyons - The neurological levels ...
Association for Project Management
Ìý
Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...
Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...
Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...
sandynavergas1
Ìý
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
Ìý
Kaun TALHA quiz Prelims - El Dorado 2025
Kaun TALHA quiz Prelims - El Dorado 2025Kaun TALHA quiz Prelims - El Dorado 2025
Kaun TALHA quiz Prelims - El Dorado 2025
Conquiztadors- the Quiz Society of Sri Venkateswara College
Ìý
POWERPOINT-PRESENTATION_DM-NO.017-S.2025.pptx
POWERPOINT-PRESENTATION_DM-NO.017-S.2025.pptxPOWERPOINT-PRESENTATION_DM-NO.017-S.2025.pptx
POWERPOINT-PRESENTATION_DM-NO.017-S.2025.pptx
MarilenQuintoSimbula
Ìý
cervical spine mobilization manual therapy .pdf
cervical spine mobilization manual therapy .pdfcervical spine mobilization manual therapy .pdf
cervical spine mobilization manual therapy .pdf
SamarHosni3
Ìý
TPR Data strategy 2025 (1).pdf Data strategy
TPR Data strategy 2025 (1).pdf Data strategyTPR Data strategy 2025 (1).pdf Data strategy
TPR Data strategy 2025 (1).pdf Data strategy
Henry Tapper
Ìý
Mate, a short story by Kate Grenvile.pptx
Mate, a short story by Kate Grenvile.pptxMate, a short story by Kate Grenvile.pptx
Mate, a short story by Kate Grenvile.pptx
Liny Jenifer
Ìý
Year 10 The Senior Phase Session 3 Term 1.pptx
Year 10 The Senior Phase Session 3 Term 1.pptxYear 10 The Senior Phase Session 3 Term 1.pptx
Year 10 The Senior Phase Session 3 Term 1.pptx
mansk2
Ìý
Storytelling instructions...............
Storytelling instructions...............Storytelling instructions...............
Storytelling instructions...............
Alexander Benito
Ìý
The Broccoli Dog's inner voice (look A)
The Broccoli Dog's inner voice  (look A)The Broccoli Dog's inner voice  (look A)
The Broccoli Dog's inner voice (look A)
merasan
Ìý
Information Technology for class X CBSE skill Subject
Information Technology for class X CBSE skill SubjectInformation Technology for class X CBSE skill Subject
Information Technology for class X CBSE skill Subject
VEENAKSHI PATHAK
Ìý
Principle and Practices of Animal Breeding || Boby Basnet
Principle and Practices of Animal Breeding || Boby BasnetPrinciple and Practices of Animal Breeding || Boby Basnet
Principle and Practices of Animal Breeding || Boby Basnet
Boby Basnet
Ìý
Digital Tools with AI for e-Content Development.pptx
Digital Tools with AI for e-Content Development.pptxDigital Tools with AI for e-Content Development.pptx
Digital Tools with AI for e-Content Development.pptx
Dr. Sarita Anand
Ìý
Lesson Plan M1 2024 Lesson Plan M1 2024 Lesson Plan M1 2024 Lesson Plan M1...
Lesson Plan M1 2024  Lesson Plan M1 2024  Lesson Plan M1 2024  Lesson Plan M1...Lesson Plan M1 2024  Lesson Plan M1 2024  Lesson Plan M1 2024  Lesson Plan M1...
Lesson Plan M1 2024 Lesson Plan M1 2024 Lesson Plan M1 2024 Lesson Plan M1...
pinkdvil200
Ìý
Research & Research Methods: Basic Concepts and Types.pptx
Research & Research Methods: Basic Concepts and Types.pptxResearch & Research Methods: Basic Concepts and Types.pptx
Research & Research Methods: Basic Concepts and Types.pptx
Dr. Sarita Anand
Ìý
A PPT Presentation on The Princess and the God: A tale of ancient India by A...
A PPT Presentation on The Princess and the God: A tale of ancient India  by A...A PPT Presentation on The Princess and the God: A tale of ancient India  by A...
A PPT Presentation on The Princess and the God: A tale of ancient India by A...
Beena E S
Ìý
TRANSFER OF PATIENTS IN HOSPITAL SETTING.pptx
TRANSFER OF PATIENTS IN HOSPITAL SETTING.pptxTRANSFER OF PATIENTS IN HOSPITAL SETTING.pptx
TRANSFER OF PATIENTS IN HOSPITAL SETTING.pptx
PoojaSen20
Ìý
TLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptx
TLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptxTLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptx
TLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptx
RizaBedayo
Ìý
APM People Interest Network Conference - Tim Lyons - The neurological levels ...
APM People Interest Network Conference - Tim Lyons - The neurological levels ...APM People Interest Network Conference - Tim Lyons - The neurological levels ...
APM People Interest Network Conference - Tim Lyons - The neurological levels ...
Association for Project Management
Ìý
Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...
Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...
Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...
sandynavergas1
Ìý
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
Ìý
POWERPOINT-PRESENTATION_DM-NO.017-S.2025.pptx
POWERPOINT-PRESENTATION_DM-NO.017-S.2025.pptxPOWERPOINT-PRESENTATION_DM-NO.017-S.2025.pptx
POWERPOINT-PRESENTATION_DM-NO.017-S.2025.pptx
MarilenQuintoSimbula
Ìý
cervical spine mobilization manual therapy .pdf
cervical spine mobilization manual therapy .pdfcervical spine mobilization manual therapy .pdf
cervical spine mobilization manual therapy .pdf
SamarHosni3
Ìý
TPR Data strategy 2025 (1).pdf Data strategy
TPR Data strategy 2025 (1).pdf Data strategyTPR Data strategy 2025 (1).pdf Data strategy
TPR Data strategy 2025 (1).pdf Data strategy
Henry Tapper
Ìý
Mate, a short story by Kate Grenvile.pptx
Mate, a short story by Kate Grenvile.pptxMate, a short story by Kate Grenvile.pptx
Mate, a short story by Kate Grenvile.pptx
Liny Jenifer
Ìý
Year 10 The Senior Phase Session 3 Term 1.pptx
Year 10 The Senior Phase Session 3 Term 1.pptxYear 10 The Senior Phase Session 3 Term 1.pptx
Year 10 The Senior Phase Session 3 Term 1.pptx
mansk2
Ìý
Storytelling instructions...............
Storytelling instructions...............Storytelling instructions...............
Storytelling instructions...............
Alexander Benito
Ìý

How to attach file using upload button Odoo 18

  • 1. How to attach file using upload button Odoo 18 Enterprise
  • 2. Enterprise Introduction In this slide, we’ll discuss on how to attach file using upload button Odoo 18. Odoo features a dedicated model, 'ir.attachments,' designed for storing attachments submitted by end users. We can see the process of utilizing the 'ir.attachments' model to enable file uploads through web forms in this slide.
  • 3. Enterprise We need to create a field in our model to handle attachments. So, let's create a many2many field in the res.partner model for storing attachments. file_attachment_ids = fields.Many2many('ir.attachment', string='Attachments') To set up the web form, we need to create an XML file. In this file, we use the 'form' tag to define the different fields we want, including the one for attaching files. The 'form' tag comes with various attributes like 'class,' 'action,' 'method,' and 'enctype.'Now, the 'enctype' attribute is particularly important when dealing with attachments. It tells the form how to handle the characters in the file. In simple terms, it ensures that the file content is not altered or encoded in a way that could cause issues when submitting it. Think of it as a special instruction to handle files properly.
  • 4. Enterprise Here's an example 'form' tag with the 'enctype' attribute: <?xml version="1.0" encoding="utf-8"?> <odoo> <template id="website_partner_form"> <t t-call="website.layout"> <div id="wrap" class="oe_structure oe_empty"> <section class="s_website_form" data- vcss="001" data-snippet="s_website_form"> <div class="container"> <form action="/form/submit" method="post" enctype="multipart/form-data"> <input type="hidden" name="csrf_token" t-att- value="request.csrf_token()"/> <div class="col-lg-7 col-md-8"> <label class="col-md-3 col-sm-4 control-label" for="att">Attach file</label> </div>
  • 5. Enterprise <div class="col-lg-7 col-md-8"> <input type="file" name="att" accept="image/*,application/pdf,video/*"/> </div> <button type="submit" class="btn btn-primary">Create </button> </form> </div> </section> </div> </t> </template> </odoo>
  • 6. Enterprise In the provided example, The 'action' attribute ("/form/submit") specifies where the form data will be sent, while the 'method' attribute ("post") ensures that this data is not visible in the URL for security reasons. The 'enctype' attribute ("multipart/form-data") is particularly crucial for handling attachments, ensuring that the file content remains intact during the submission process. Notably, the 'csrf_token' serves as a security measure to guard against Cross-Site Request Forgery (CSRF) attacks. Within the form, a user-friendly section for attaching files is created using the 'label' and 'input' elements. The 'name' attribute ("att") is employed to identify the attached data, and the 'accept' attribute restricts file types to images, PDFs, and videos. Finally, the 'button' element initiates the form submission, commencing the process of creating and saving the submitted data.
  • 7. Enterprise This will create a file input field like this. We can add the file by clicking on choose file button.
  • 8. Enterprise For attaching files to partners, simply select the desired file and click "Create." This action will add the file as an attachment to the corresponding field in the partner record.
  • 9. Enterprise In the controller, we need to create a function that will handle encoding the file and then saving it to the database. Below is a sample code: import base64 from odoo import http from odoo.http import route, request, Controller class FileUpload(http.Controller): @http.route('/partner/form', type='http', auth='public', website=True) def file_upload(self, redirect=None, **kw): current_partner_id = request.env.user.partner_id file_name = kw.get('att').filename file = kw.get('att')
  • 10. Enterprise attachment_id = request.env['ir.attachment'].create({ 'name': file_name, 'type': 'binary', 'datas': base64.b64encode(file.read()), 'res_model': current_partner_id._name, 'res_id': current_partner_id.id }) current_partner_id.update({ 'file_attachment_ids': [(4, attachment_id.id)], }) return request.render('custom_,module.website_partner_form')
  • 11. Enterprise In the partner record, we can see the attachment added in the many2many field.
  • 12. For More Info. Check our company website for related blogs and Odoo book. Check our YouTube channel for functional and technical videos in Odoo. Enterprise www.cybrosys.com