Im hoping someone can help me with this as I am little lost. So, here goes. I have a model which has a file field. I am creating multiple instances of the model object and then doing a bulk_create at the end.
How can I set the FileField when I create the model instances? Ive tried setting the field to the file name, tried other methods but it never works, I have no idea what Im doing wrong. Any insight?
So, as I mentioned I am using PyPdf2 and breaking up a PDF passed in into smaller files with this code:
After this I will loop over and create multiple Model objects which are stored in an array something like this:
doc_objs = []
Here’s how the model object in question is laid out:
The LAST line of the model is the one I am having an issue with. I cannot figure out how to get the PDF file in the first paste associated with the model instance in the second paste.
I hope this helps and thanks for taking the time to assist.
In the future, please post the code snippets here. When you do, enclose it between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```.
Anyway, you didn’t post your model, so I’m guessing that the field named pdf is your FileField.
So this process would involve creating an instance of a FileField object and assigning the right attributes to it.
Disclaimer: I’ve never actually done this myself. (When we generate files, we store the relative path in a CharField, bypassing all the extra work done by the Django file interfaces. It’s a sufficiently small part of what we do that we decided it wasn’t worth the effort to worry about doing anything else.)