0

In my app I parse some xml which contains a path to an image file. Now if I am passing the path to the property of my model which is a FileField it's not copying the file using the upload_to settings. I also tried to pass it a stream of that file but that raised an exception.

How do I use the FileField with data that isn't coming from a request?

1

1 Answer 1

1

Assuming the file is in your MEDIA_ROOT (If it's outside of MEDIA_ROOT you'll get SuspiciousOperation errors):

m = YourModel(file='uploads/file.txt')

If you already have the file on your system, it'd surely be easier to just move it to your uploads directory. You could always customize FileField to handle moving the file for you.

Sign up to request clarification or add additional context in comments.

1 Comment

Does that mean if the file isn't in the MEDIA_ROOT I have to manually move it myself first? Means I would be recreating the upload_to logic which isn't very DRY.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.