Home > OS >  ODOO - Call method/attribute from model inside Template XML
ODOO - Call method/attribute from model inside Template XML

Time:12-29

[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  style="cursor: pointer"
               autofocus="autofocus" aria-label="Notification Badge"
               accesskey="h">
                <i role="img" aria-label="Notifications" />
                <t>
                    <span >
                        <!--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  style="cursor: pointer"
               autofocus="autofocus" aria-label="Notification Badge"
               accesskey="h">
                <i role="img" aria-label="Notifications" />
                <t>
                    <span >
                        <t t-esc="notification_count"/>
                    </span>
                </t>
            </a>
        </t>
    </t>
</templates>

Can anyone help me? please

CodePudding user response:

I resolved this problem, everyone can do like steps below: To get value from python model into Template XML, we need through JavaScript

Step 1: Create function return value inside model python like below:

@api.model
def get_notification_count_unread(self):
    notification_count_unread = self.env['notification.notification.public'].search_count([])
    return notification_count_unread

Step 2: Use JS to query value of above function like below:

  _getNotificationCountUnread: function () {
    var self = this;
    return self._rpc({
        model: 'notification.notification.public',
        method: 'get_notification_count_unread',
        args: [],
    }).then(function (data) {
        self.$('.o_NotificationBadge_counter').text(data.toString());
    });
},

Step 3: call js inside template xml through class of xml:

CodePudding user response:

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.

  • Related