You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo 16 Chatter中附件上传/创建日期显示问题求助

在Odoo 16 Chatter中显示附件创建日期的问题排查与解决

我想在Odoo 16的Chatter里显示附件的上传/创建日期,以此管控版本上传,尝试了自定义attachment.js、attachment.xml、attachment_thread.js文件,但附件创建日期仍未显示,求解决办法。

已尝试的自定义代码

attachment.js

/** @odoo-module **/
import { clear } from '@mail/model/model_field_command';
import { attr } from '@mail/model/model_field';

import { registerPatch } from '@mail/model/model_core';

registerPatch({
    name: 'Attachment',
    modelMethods: {
        /**
         * @override
         */
        convertData(data) {
            const res = this._super(data);
            if ('create_date' in data) {
                res.create_date = data.create_date;
            }
            return res;
        },
    },
    fields: {
        create_date: attr({
            compute() {
                const create_date = this.create_date;
                if (create_date) {
                    return create_date;
                }
                return clear();
            },
        }),
    },
});

attachment.xml

<?xml version="1.0" encoding="UTF-8"?> 
<templates xml:space="preserve"> 
    <t t-name='catus.custome_attachment' t-inherit="mail.AttachmentCard" t-inherit-mode="extension" owl="1" primary="1">
    <xpath expr="//*[hasclass('o_AttachmentCard_filename')]" position="after">
    Date Uploaded: 
    
        <div class="o_AttachmentCard_createdate" t-if="attachmentCard.attachment.create_date">
            <t t-esc="attachmentCard.attachment.create_date"/>
        </div>
    </xpath>
    <xpath expr="//*[hasclass('o_AttachmentCard_extension')]" position="replace">            
            <div></div>
        </xpath> 
    </t>
</templates>

attachment_thread.js

/** @odoo-module **/

import { registerPatch } from '@mail/model/model_core';
import { insertAndReplace } from '@mail/model/model_field_command';

registerPatch({
    name: 'Thread',
    modelMethods: {
        async fetchData( requestList) {
            //get create_date to show
            this._super.apply(this, requestList);
            let attachmentsData = await this.async(() => this.env.services.rpc({
                model: 'ir.attachment',
                method: 'search_read',
                domain: [
                    ['res_id', '=', this.id],
                    ['res_model', '=', this.model],
                ],
                fields: ['id', 'name', 'mimetype', 'create_date'],
                orderBy: [{name: 'id', asc: false}],
            }, {shadow: true}));
            this.update({
                originThreadAttachments: insertAndReplace(attachmentsData),
            });
            this.update({ areAttachmentsLoaded: true });
        }
    },
});

问题排查与解决步骤

1. 修复Attachment模型字段逻辑

create_date字段无需compute逻辑,当前写法会导致循环依赖,直接定义为普通属性即可:
修改attachment.js的fields部分:

fields: {
    create_date: attr(), // 移除compute,定义为普通属性
},

2. 修正Thread.fetchData的异步逻辑

原代码中_super调用未等待完成,且分两次update可能导致数据更新不及时,调整为:

async fetchData(requestList) {
    await this._super(...requestList); // 等待父方法执行完成
    const attachmentsData = await this.async(() => this.env.services.rpc({
        model: 'ir.attachment',
        method: 'search_read',
        domain: [['res_id', '=', this.id], ['res_model', '=', this.model]],
        fields: ['id', 'name', 'mimetype', 'create_date'],
        orderBy: [{name: 'id', asc: false}],
    }, {shadow: true}));
    this.update({
        originThreadAttachments: insertAndReplace(attachmentsData),
        areAttachmentsLoaded: true,
    });
}

3. 验证XML模板的匹配与渲染

  • 用浏览器开发者工具查看原始AttachmentCard的DOM结构,确认o_AttachmentCard_filename类存在,若路径不匹配则调整xpath表达式。
  • 简化模板调试,先去掉t-if判断,直接输出值排查数据是否传递:
<xpath expr="//*[hasclass('o_AttachmentCard_filename')]" position="after">
    <div class="o_AttachmentCard_createdate">
        Date Uploaded: <t t-esc="attachmentCard.attachment.create_date || '无数据'"/>
    </div>
</xpath>

4. 清除缓存与验证模块加载

  • 清除Odoo资产缓存:进入「设置->技术->资产管理->清除缓存」,同时清空浏览器缓存。
  • 确认自定义模块已正确安装、更新,且用户拥有ir.attachment模型create_date字段的读取权限。

5. 调试数据传递

在attachment.js的convertData方法中加入日志,查看后端返回数据:

convertData(data) {
    const res = this._super(data);
    console.log('Attachment原始数据:', data); // 检查是否包含create_date
    if ('create_date' in data) {
        res.create_date = data.create_date;
    }
    return res;
}

在XML模板中加入调试输出,查看Attachment对象结构:

<t t-debug="attachmentCard.attachment"/>

通过浏览器控制台确认create_date属性是否存在。

内容的提问来源于stack exchange,提问作者An An

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 13:07:41