python - 当我在 Odoo 中上传图像时,如何在对话框窗口中按图像格式进行过滤?

标签 python xml image format odoo

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

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

在 View 内显示它:

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

这将打开一个操作系统对话框窗口,允许我选择图像文件。问题是我只需要使用 *.jpg 文件,并且我不知道如何打开用此类文件过滤的对话框窗口。

最佳答案

我将向您描述我必须执行的所有程序,以了解您应该做什么。这对于您下次想要修改其他任何内容时可能很有用:

检查

如果您检查小部件的 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>

关于python - 当我在 Odoo 中上传图像时,如何在对话框窗口中按图像格式进行过滤?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49488146/

相关文章:

sql-server - 如何在 SQL Server 中删除表中的随机换行符到 XML 文件

xml - R 上包 ‘XML’ 的编译失败

java - 读取多页 Tiff 图像并用 Java 写入 pdf

python - 可以在 scikit-learn 中使用预先计算的内核从 SVM 绘制 ROC 图吗?

python - Keras np_utils.to_categorical 行为不同

xml - XPath:是否可以组合查询

javascript - 如何在 JavaScript 中保存一组特定的图像类别?

python - 如何将 +1 添加到函数之间的值

python - 使用 os.path.join 构建向上路径时,如何避免连续出现大量 os.pardir?

javascript - HTML5 Canvas - 轮子旋转功能行为异常