[ODOO] How to call method/attribute from python model in Template XML?
I have template xml below:
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">
<t t-extend="Menu">
<t t-jquery=".o_menu_sections" t-operation="after">
<a class="o_notification_badge" style="cursor: pointer"
autofocus="autofocus" aria-label="Notification Badge"
accesskey="h">
<i role="img" aria-label="Notifications" class="fa fa-bell"/>
<t>
<span class="o_NotificationBadge_counter badge badge-pill">
<!--number of records-->
</span>
</t>
</a>
</t>
</t>
</templates>
I have .py model below:
class NotificationNotificationPublic(models.Model):
_name = 'notification.notification.public'
name = fields.Char(string='Title')
notification_count = fields.Integer(compute="_compute_notification_count")
def _compute_notification_count(self):
for record in self:
record.notification_count = self.env['notification.notification.public'].search_count([])
Src below i tried but not working:
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">
<t t-extend="Menu">
<t t-jquery=".o_menu_sections" t-operation="after">
<a class="o_notification_badge" style="cursor: pointer"
autofocus="autofocus" aria-label="Notification Badge"
accesskey="h">
<i role="img" aria-label="Notifications" class="fa fa-bell"/>
<t>
<span class="o_NotificationBadge_counter badge badge-pill">
<t t-esc="notification_count"/>
</span>
</t>
</a>
</t>
</t>
</templates>
Can anyone help me? please
You can try to call function from template itself. Here is an example.
<t t-set="name_of_variable" t-value="request.env['your.model.name'].function_name(arguments)"/>
<span t-if="name_of_variable" > <!--Display only somethings--> </span>
</t>
name_of_variable is variable in which will be saved returned value from called function. You can also use for loop above "t element" so you are able call function with different arguments (1,2,3,...). Than you can do something like this: <span t-if="(argument == 0 and name_of_variable )" >
. and name_of_variable
is only when python function returns True.