当我在 Odoo 中上传图片时,如何在对话框 window 上按图片格式进行过滤?

How can I filter by image format on the dialog window when I upload an image in Odoo?

我有一个带有图像属性的模型:

image = fields.Binary(
    string="Imagen",
    required=True
)

在视图中显示:

<field name="image" widget="image" />

这将打开一个操作系统对话框 window,允许我 select 图像文件。问题是我只需要 *.jpg 个文件,我不知道如何打开这个用这种文件过滤的对话框 windows。

我将向您描述我必须执行的所有程序,以找出您应该执行的操作。这对您下次要修改任何其他内容时可能会有用:

检查

如果您检查小部件的 html,您可以找到隐藏的输入文件:

<input accept="image/*" class="oe_form_binary_file" name="ufile" type="file">

因此,如果您转到 this w3schools 页面,您可以查看 accept="image/*" 的含义。我们必须用 .jpg 或您需要的任何其他扩展覆盖它。

查找主要源代码

那么你应该在原始Odoo源代码上寻找模板。正如您在 web 模块中看到的,使用的模板如下:

<t t-name="HiddenInputFile">
    <div t-attf-class="oe_hidden_input_file #{fileupload_class or ''}" t-att-style="fileupload_style">
        <form class="oe_form_binary_form" t-att-target="fileupload_id"
            method="post" enctype="multipart/form-data" t-att-action="fileupload_action || '/web/binary/upload'">
            <input type="hidden" name="session_id" value="" t-if="widget.session.override_session"/>
            <input type="hidden" name="callback" t-att-value="fileupload_id"/>
            <t t-raw="0"/>
            <input type="file" class="oe_form_binary_file" name="ufile" t-if="widget.widget!='image'"/>
            <input type="file" class="oe_form_binary_file" name="ufile" accept="image/*" t-if="widget.widget=='image'"/>
        </form>
        <iframe t-att-id="fileupload_id" t-att-name="fileupload_id" style="display: none"/>
    </div>
</t>

扩展和修改原始代码

最后你只需要用 Qweb Template Inheritance

覆盖输入字段
<templates id="image_widget_jpg_template" xml:space="preserve">
    <t t-extend="HiddenInputFile">
        <t t-jquery="[accept='image/*']" t-operation="replace">
            <input type="file" class="oe_form_binary_file" name="ufile" accept=".jpg" t-if="widget.widget=='image'"/>
        </t>
    </t>
</templates>