2

I work with file storage module and need to upload files to module. In my definition i use

class TestClass(models.TransientModel):
_name = "module_name.test_class"

  attachment_id = fields.Many2one(
    comodel_name='ir.attachment',
    string="newFileVersion",
    index=True,
    copy=False
 )

When i use in xml

<field name="attachment_id"/>

it shows input dropdown item with alredy loaded in DB files, but I need to choose files from my local computer. What I did wrong. (Sorry for bad english)

Umks
  • 55
  • 2
  • 8

2 Answers2

2

CZoellner, I found another one decision: in py

file = fields.Binary("Attachment")
file_name = fields.Char("File Name")

in xml

<field name="file" filename="file_name"/>
<field name="file_name"/>

in that case file_name store real filename as it store on the computer. And then just write() to ir.attachment right name. Reely wize mans said that to ask right question you need ton know at leest half of the answer

Umks
  • 55
  • 2
  • 8
1

You should use Binary field type and attachment=True to let Odoo tread the files as normal attachments (using the Odoo file storage).

my_file = fields.Binary(string="My File", attachment=True)

attachment=True is the default in Odoo 13 so if you want to save the binary data in database you have to set it to False.

For images there is a field type Image.

CZoellner
  • 13,553
  • 3
  • 25
  • 38
  • this not very good decision. Yes at view we can choose file from local computer, but what can I do with my_file binary field? I need to store file and get all attributes of file like: name, res_id, create_uid ... – Umks Jan 24 '20 at 11:36
  • You should listen to him and If you want to retrieve the ir.attachment attrs you can search for it, ` using this domain [ ('res_model', '=', record._name), ('res_field', '=', 'field_name'), ('res_id', '=', record.id), ]` using this the information you can find all these information, up vote for this recommended answer – Charif DZ Jan 24 '20 at 14:55
  • @Umks where and for what do you need this information (res_id, create_uid, etc.)? Because technically this information is there. Odoo creates a real `ir_attachment` database row, if you have defined `attachment=True` on the binary field. – CZoellner Jan 24 '20 at 16:00
  • Wen I try to do somethig like this "if alredy_have_file.name = my_file.name:" it's error, becose my_file don't have any fields. Sorry if i don't get it. – Umks Jan 24 '20 at 17:16
  • you was right, it's all there just need to search, but these anoter one problem my_file = fields.Binary(string="My File", attachment=True) change the field name in ir.attachment to my_file, is there any way to not change ir.attachment.name – Umks Jan 27 '20 at 07:16
  • I don't understand the question, can you give an example please? – CZoellner Jan 27 '20 at 08:04
  • He just gave an example, just do `any_name_you_like = fields.Binary(......., attachment=True)` the key is to use `attachement=True` the name of your field can be anything you want – Charif DZ Jan 27 '20 at 08:49
  • When i attach file this way it write to DB ir.attachment with field name as my_file, as you say anything I want, but I need real name like asd.pdf, as it names on my computer – Umks Jan 27 '20 at 09:13
  • 1
    Okay, now i understand. I think that's not possible with this solution. You might want to use the mixin mail.thread on your model. Don't forget to change the form view and add the chatter. The chatter has a nice attachment widget, which should be enough for your requirement. – CZoellner Jan 27 '20 at 09:29
  • Ok, big thanks for give me direction where to find. – Umks Jan 27 '20 at 09:51