Шаблон Odoo не видит полей в main_object - PullRequest
0 голосов
/ 15 апреля 2019

Я использую класс website.seo.metadata для расширения моей модели.

В модель добавлено поле noindex :

class service(models.Model):
    _name = 'pr_filials.service'
    _inherit = ['mail.thread', 'website.seo.metadata', 'website.published.mixin']

    name = fields.Char(string="Name", required=True)
    noindex = fields.Boolean(string="Noindex", default=False)
....

В шаблоне я использую такой вид:

<template id="assets_frontend_noindex" inherit_id="website.assets_frontend" name="Noindex">
            <xpath expr="." position="inside">
                <t t-if="main_object and 'noindex' in main_object">
                    <t t-if="main_object.index">
                        <meta name="robots" content="noindex" />                        
                    </t>
                </t>
            </xpath>
</template>

Но поле шаблона не видноnoindex

Я тоже пытался расширить класс website.seo.metadata.Ничего не изменилось.

В заголовке присутствует такая информация:

<html lang="ru-RU" data-website-id="1" data-editable="1" data-view-xmlid="882" data-main-object="pr_filials.service(13,)" data-oe-company-name="SiteName"> 

Все работает нормально, если я пытаюсь подключить имя поле - данные из него отображаются.

Как получить данные из моего поля noindex в заголовке?

ОБНОВЛЕНИЕ: Я расширил ir.ui.view :

class view(osv.osv):
    _name = "ir.ui.view"
    _inherit = "ir.ui.view"
    _columns = {
        'noindex': fields.boolean("Noindex"),
    }

В класс сервиса добавлен такой код:

@api.onchange('noindex')
def _set_noindex(self):
    self.website_meta_noindex = self.noindex
    url = u'/uslugi/' + self.url_name
    _logger.warning('URL: %s', url)
    self.write_to_ui_view(url=url, noindex=self.noindex)

def write_to_ui_view(self, cr, uid, url, noindex=False, ispage=True, template='pr_filials.servicepage', context=None):
    _logger.error('write_to_ui_view')
    context = context or {}
    imd = self.pool.get('ir.model.data')
    view = self.pool.get('ir.ui.view')
    template_module, template_name = template.split('.')

    # completely arbitrary max_length
    page_name = url
    page_xmlid = "%s.%s" % (template_module, page_name)

    _, template_id = imd.get_object_reference(cr, uid, template_module, template_name)
    website_id = context.get('website_id')
    key = template_module+'.'+page_name
    page_id = None

    if view.search(cr, openerp.SUPERUSER_ID, [('key', '=', key)]):
        page_id = view.search(cr, openerp.SUPERUSER_ID, [('key', '=', key)])[0]
    else:
        page_id = view.copy(cr, uid, template_id, {'website_id': website_id, 'key': key}, context=context)
    page = view.browse(cr, uid, page_id, context=dict(context, lang=None))
    page.write({
        'arch': page.arch.replace(template, page_xmlid),
        'name': page_name,
        'page': ispage,
        'noindex': noindex
    })
    return page_xmlid

Теперь я могу использовать noindex, зарегистрированный в main_Object.Но каждый раз возвращают Ложь.В БД присутствует True, но в поле noindex возвращается False ...

Я пытаюсь проверить значение noindex на странице.Для этого я добавил метатег в заголовок и преобразовал noindex в строку:

<meta name="RB" content="noindex" ><t t-raw="str(main_object.noindex)"/></meta>

Здесь представлен результат: enter image description here

В БД эта запись имеет значение True: enter image description here

1 Ответ

0 голосов
/ 15 апреля 2019

Сохраняйте это просто ...

Я храню его с помощью шаблона qweb:

<template id="layout" inherit_id="website.layout">
                <xpath expr="html/head" position="inside">
                    <t t-if="page_data.noindex">
                            <meta name="robots" content="noindex" />
                    </t>
                </xpath>
</template>

<template id="servicepage">
            <t t-call="pr_filials.layout">...</t>
</template>

и мне не нужен какой-либо измененный класс.

...