How to Store Data on the Odoo 17 Website

CelineGeorge1 816 views 11 slides Jul 04, 2024
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

Here we are going to discuss how to store data in Odoo 17 Website.
It includes defining a model with few fields in it. Add demo data into the model using data directory. Also using a controller, pass the values into the template while rendering it and display the values in the website.


Slide Content

Storing Data on Odoo 17 Website Enterprise

Introduction Enterpr ise Here we are going to discuss how to store data in Odoo 17 Website. It includes defining a model with few fields in it. Add demo data into the model using data directory. Also using a controller, pass the values into the template while rendering it and display the values in the website.

Enterprise We’ve created a custom module ‘travel_management’ with the following structure.

Enterprise First, in the module, we have to created ‘models’ directory in which the custom model is defined. Here, for the travel booking, we have the class and attributes as class TravelBooking(models.Model): _name = 'travel.booking' _description = 'Travel Card' _check_company_auto = True partner_name = fields.Char(string="Partner Name", required=True) partner_mobile = fields.Char(string="Mobile") passenger_count = fields.Integer(string="No of Passengers")

Enterprise Now, create menu in the website using the code below . Here, it is saved in the ‘menu.xml’ file in the ‘data’ directory. <record id="menu_booking" model="website.menu"> <field name="name">Booking</field> <field name="url">/book_travel</field> <field name="parent_id" ref="website.main_menu"/> <field name="sequence" type="int">20</field> </record> This just creates a menu ‘Booking’ in the website frontend of Odoo. I’ve created another menu called ‘Travel Spots’ also. The file menu.xml must be specified in the manifest.py file also.

Enterprise After that, we can pass few data to this model by creating records through code. This is also saved in the same ‘menu.xml’ file in the ‘data’ directory. <record id="travel_booking_1" model="travel.booking"> <field name="partner_name">Sam</field> <field name="partner_mobile">989898951</field> <field name="passenger_count">55</field> </record> <record id="travel_booking_2" model="travel.booking"> <field name="partner_name">Alisha</field> <field name="partner_mobile">8945145150</field> <field name="passenger_count">100</field> </record> This code creates two records with the given field values when the module is getting installed.

Enterprise Now, from the controllers, we can pass these values using the template that we design. The controller ‘main.py’ file from odoo import http from odoo.http import request class TravelData(http.Controller): @http.route('/book_travel', type='http', auth="public", website=True) def travel_booking_info(self, **arg): booking_detail=request.env['travel.booking'].sudo().search([]) values = {'booking': booking_detail} return request.render( 'travel_management.booking_details', values)

Enterprise Here, the http controller is defined with a method to fetch data from backend to show on the website. The details will be shown using a template. Inside the method, we are searching for records from the model 'travel.booking'. And all those records is getting saved in the ‘booking_detail’ variable which is then passed as a dictionary value while rendering the template. The template is called from the controller using the format ‘module_name.template_name’. Here in this example, the template name is ‘booking_details’.

Enterprise The code for the template is saved in ‘booking.templates.xml’ file under ‘views’ directory and it has the code as follows <template id="booking_details" name="Travel Booking"> <t t-call="website.layout"> < t t-set="title">Travel Booking Details</t> <div class="oe_structure oe_empty"> <div class="container"> <t t-set="i" t-value="1"/> <t t-foreach="booking" t-as="o"> <div> <h3> Booking <t t-esc="i"/></h3> <h5 t-field="o.partner_name"/> <h5 t-field="o.partner_mobile"/> <h5 t-field="o.passenger_count"/> </div> <t t-set="i" t-value="i+1"/> </t> </div> </div> </t> </template>

Enterprise Upon upgrading the module and going to the website module and clicking on the ‘Homepage’, We can see the menus that we’ve created. Aslo, under the ‘Booking’ menu, the data that we’ve created using the code and designed using the xml template.

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