Skip to Content
Menu
This question has been flagged
2 Replies
378 Views

Hi guys, I cannot configure Calendar to show duration of the event in Calendar view

I use Odoo 18.


p.s. I want to inset examples, but Odoo edit form doesn't allow me to do so...

This is what I have: https://shottr.cc/s/1fd6/SCR-20250717-f5x





class MyEvent(models.Model):

    _name = 'mymodule.my_event'

    _description = 'My Event'


    name = fields.Char("Title", required=True)


    date_start = fields.Datetime("Start", required=True)

    date_stop = fields.Datetime("End", required=True)


    priority = fields.Selection([

        ('low', 'Low'),

        ('normal', 'Normal'),

        ('high', 'High')

    ], string="Priority")


This is my XML for calendar view:


<odoo>

    <record id="view_mymodule_my_event_calendar" model="ir.ui.view">

        <field name="name">view_mymodule_my_event_calendar</field>

        <field name="model">mymodule.my_event</field>

        <field name="arch" type="xml">

            <calendar

                    string="My Calendar"

                    date_start="date_start"

                    date_stop="date_stop"

                    color="priority"

                    mode="week"

                    event_open_popup="true">

                <field name="name"/>

                <field name="priority"/>

            </calendar>

        </field>

    </record>

</odoo>


Please Help!



Avatar
Discard
Best Answer

Hii,

Here is updated code please try this 

Python Model (models/my_event.py)

from odoo import models, fields class MyEvent(models.Model): _name = 'mymodule.my_event' _description = 'My Event' name = fields.Char("Title", required=True) date_start = fields.Datetime("Start", required=True) date_stop = fields.Datetime("End", required=True) priority = fields.Selection([ ('low', 'Low'), ('normal', 'Normal'), ('high', 'High') ], string="Priority", default='normal')

XML View File (views/my_event_views.xml)

<odoo> <!-- Calendar View --> <record id="view_my_event_calendar" model="ir.ui.view"> <field name="name">my_event.calendar</field> <field name="model">mymodule.my_event</field> <field name="arch" type="xml"> <calendar string="My Events" date_start="date_start" date_stop="date_stop" color="priority" event_open_popup="true" > <field name="name"/> </calendar> </field> </record> <!-- Tree View --> <record id="view_my_event_tree" model="ir.ui.view"> <field name="name">my_event.tree</field> <field name="model">mymodule.my_event</field> <field name="arch" type="xml"> <tree> <field name="name"/> <field name="date_start"/> <field name="date_stop"/> <field name="priority"/> </tree> </field> </record> <!-- Form View --> <record id="view_my_event_form" model="ir.ui.view"> <field name="name">my_event.form</field> <field name="model">mymodule.my_event</field> <field name="arch" type="xml"> <form> <sheet> <group> <field name="name"/> <field name="date_start"/> <field name="date_stop"/> <field name="priority"/> </group> </sheet> </form> </field> </record> <!-- Action --> <record id="action_my_event" model="ir.actions.act_window"> <field name="name">My Events</field> <field name="res_model">mymodule.my_event</field> <field name="view_mode">calendar,tree,form</field> <field name="view_id" ref="view_my_event_calendar"/> <field name="help" type="html"> <p class="o_view_nocontent_smiling_face"> Create your first event </p> </field> </record> <!-- Menu --> <menuitem id="menu_my_event_root" name="My Module"/> <menuitem id="menu_my_event_main" name="My Events" parent="menu_my_event_root" action="action_my_event"/> </ odoo >

Manifest (__manifest__.py)

Ensure you include the XML file in data:

{ 'name': 'My Custom Calendar Module', 'version': '1.0', 'depends': ['base'], 'data': [ 'views/my_event_views.xml', ], 'application': True, }

I hope it is of full use.

Avatar
Discard
Author

Thank you for your reply, but is has the same problem:

**in a Day or Week View I do not not see event duration**

Please see these links to understand the problem:

Here is what I have on a week view: https://shottr.cc/s/1QyA/SCR-20250717-f4n
Here is what I want/expect to see: https://shottr.cc/s/1fd6/SCR-20250717-f5x

Hii
Update the Python Model
python
Copy
Edit
from odoo import models, fields, api
from datetime import timedelta

class MyEvent(models.Model):
_name = 'mymodule.my_event'
_description = 'My Event'

name = fields.Char("Title", required=True)
date_start = fields.Datetime("Start", required=True)
date_stop = fields.Datetime("End", required=True)
priority = fields.Selection([
('low', 'Low'),
('normal', 'Normal'),
('high', 'High')
], string="Priority", default='normal')

duration = fields.Float("Duration", compute="_compute_duration", store=True)

@api.depends('date_start', 'date_stop')
def _compute_duration(self):
for rec in self:
if rec.date_start and rec.date_stop:
delta = rec.date_stop - rec.date_start
rec.duration = delta.total_seconds() / 3600.0 # in hours
else:
rec.duration = 0
Update Calendar View to Use duration
Update your calendar tag in my_event_views.xml to include:

xml
Copy
Edit
<calendar
string="My Events"
date_start="date_start"
date_stop="date_stop"
duration="duration"
color="priority"
event_open_popup="true"
>
<field name="name"/>
</calendar>
please try this and upgrade your module
i hope it is usefull

Best Answer

Hi,

Please check this blog. In this blog it clear give to add calendar view 

https://www.cybrosys.com/blog/how-to-create-calendar-view-in-odoo-18


Hope it helps

Avatar
Discard
Author

Hi Cybrosys!

I checked many of your tutorials thank you for that.

But your link doesn't help for my question. Please help.

Related Posts Replies Views Activity
0
Jun 20
2130
0
Mar 15
3648
1
Mar 15
5085
1
Jul 25
387
2
Jun 25
770