Add a Calendar View
In Odoo, we have different types of views. the best way to define the object to the user is by using a View. Form, Tree, Kanban, Gantt, and Calendar are some of them. here will explain the calendar view in odoo. Odoo Calendar view provides a systematic timeline/schedule for the data information in a business organization based on Day, Week, and Month.
Here is a sample code on calendar view from the example.xml class.
<!-- Calendar View--> <record model="ir.ui.view" id="session_calendar_view"> <field name="name">excredit.calendar</field> <field name="model">library_basic.excredit</field> <field name="arch" type="xml"> <calendar string="Credit Calendar" date_start="start_date" date_stop="end_date"color="user_id"> <field name="borrower"/> </calendar> </field> </record>
After adding the code above to the class.xml don't forget to add a view model name such as 'calendar' inside the tag <field></field> like:
For the complete code in .xml it's like this:
<!-- actions opening views on models-->
<record model="ir.actions.act_window" id="library_basic.excredit_action_window">
<field name="name">Book Borrowing List</field>
After adding the XML code, then continue to model .py to get some fields to show data in the XML class. for example code in the .py model is like this:
here add the start date and end date to get the date that will be displayed in the Calendar and the duration will define the distance between the start date and the end date
start_date is set to the current date as default on the form page.
end_date call the method _get_end_date and _set_end_date.
start_date = fields.Date(string='Start Date', default=datetime.now().strftime('%Y-%m-01'))
end_date = fields.Date(string='End Date', compute='_get_end_date', inverse='_set_end_date')
duration = fields.Float(string='Duration')
After adding the code above, continue creating some method that called on end_date about how many duration have been input to range the start date and end date itself.
for r in self:
if not (r.start_date and r.end_date):
r.duration = (r.end_date - r.start_date).day + 1
After creating method _set_end_date then continue to get a date that has been declared, for example, the code is like this:
for r in self:
if not (r.start_date and r.duration):
r.end_date = r.start_date
duration = timedelta(days=r.duration, seconds=-1)
r.end_date = r.start_date + duration
Code about @api.depends('start_date') depends on the field about set the current date.
After all, the code is done and added then Look for the model that has a calendar icon its looks like the image below, in the top right of the page, there is a calendar icon that has been added to the code before.
When clicking on the calendar icon that will open new pages such as the calendar page which display the data that have a date and time.
Here it is explained when the event starts and ends
In the calendar view, there is also a choice between the day of the week and the month and the position is top left in the calendar view, like the following example:
The XML code described above can be accessed on this page. And for the Py on this page.
Odoo has several views, one of which is the calendar, in the calendar you can add the schedule of an event or the date of a field, and a date can be customized as you wish, in the calendar view is very informative and can be selected by day, week and month, In the calendar, we can also go directly to the data fields in the calendar view, such as going to event details and so on.