Changing the Filename of a Uploaded Binary File Field

荒凉一梦 提交于 2019-12-05 12:31:50
Black and White

My Solution to this matter/problem, create first a compute field and its function

.py

filename = fields.Char('file name', readonly = True,store = False,compute ='legacy_doc1_getFilename')

@api.one
def legacy_doc1_getFilename(self):

    if len(self.employee_number) > 0:
        self.filename = str(self.employee_number) + '_ConfidentialReports.pdf'
    else:
        self.filename = 'filename_ConfidentialReports.pdf'

and in XML file just add the attribute file and the field

<page string="Legacy Documents">
    <group>
        <field name="filename" readonly="1" invisible="1"/>
        <field name="legacy_doc_1" filename="filename"/>
    </group>
</page>

I had the same problem and the following solution sovled it:

class MyModel(models.Model):
    _name = 'my.model'

    name = fields.Char(string='Name')
    image = fields.Binary(string='Image', required=True)
    image_filename = fields.Char(string='Image Filename')

in xml:

<field name="name" />
<field name="image_filename" invisible="1"/>
<field widget="binary" height="64" name="image" filename="image_filename" />

And, here is the result:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!