Hi all developers, I would like to upload only .pdf file , so what can I do ? if my field like this :
file = fields.Binary(string='file', attachment=True)
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
Hi all developers, I would like to upload only .pdf file , so what can I do ? if my field like this :
file = fields.Binary(string='file', attachment=True)
Thank you , Ravi Gadhia and Thank you Zbik ..Now I can solve with this :
In Model,
file = fields.Binary(string='file', attachment=True)
file_name = fields.Char("File Name")
@api.constrains('file')
def _check_file(self):
if str(self.file_name.split(".")[1]) != 'pdf' :
raise ValidationError("Cannot upload file different from .pdf file")
In View,
<field name="file" filename="file_name"/>
<field name="file_name" invisible="1"/>
=> Help to vote if this solve your problem too
Hello, I have a little supplement after reading the first answer. To be on the safe side, the module will receive a different filename, and we should use the .endwith function to determine the end of the file.
As shown in the following code:
In model:
file = fields.Binary(string='file', attachment=True)
filename = fields.Char()
@api.constrains('file', 'filename')
def get_data(self):
if not self.filename.endswith('.pdf'): # check if file pdf
raise ValidationError('your error message')
else:
pass
In views:
<field name="file" filename="filename"/>
<field name="filename" invisible="1"/>
Unfortunately, there is no built-in option to add accept param by binary field definition so you have to do customization for it.
Good news is that in master new PR landed in the master to achieve it and available in next versions 14.0 :)
ref: https://github.com/odoo/odoo/pull/38351
Note: it's a client-side option so user still can upload any file by removing the filter from browse window. if you want strict restriction then detect file type from binary content at server side
Thanks :)
Use the constrains decorator to examine the type of data being loaded. See constrains documentation
Example:
@api.constrains('name', 'description') def _check_description(self): if self.name == self.description: raise ValidationError("Fields name and description must be different")
Create an account today to enjoy exclusive features and engage with our awesome community!
Sign up