From an operation, we can change the binary associated to a document.
This is possible by redefining the value of the "file" attribute.
Example code when we have the exact path to the new binary
new_path = '/var/www/athentose/media/uploads/repo1/documento.pdf'
doc.file = new_path
doc.save(update_fields=['file'])
Example of code when we have another document with the binary
In this example, we have two documents, doc1 and doc2 and we assign the binary of doc2 to doc1.
doc1.file = doc2.file
doc1.save(update_fields=['file'])
IMPORTANT: If the binary of a document is to be replaced, it is good practice to leave a trace of the path of the original binary. This can be done by saving a feature of the document. For example:
doc1.set_feature('binario.anterior', doc1.path())
doc1.file = doc2.file
doc1.save(update_fields=['file'])
Comments
0 comments
Please sign in to leave a comment.