开发者

Locating file path from a <InMemoryUploadedFile> Django object

开发者 https://www.devze.com 2022-12-28 16:52 出处:网络
I have a Django app which, submitting a package, should return values that are inside it.. Submitted the form to a view called \"insert\":

I have a Django app which, submitting a package, should return values that are inside it.. Submitted the form to a view called "insert":

request.FILES['file']

returns the file objects, but it is of kind < InMemoryUploadedFile>. What i need is a way to get the absolute path of the u开发者_如何学Pythonploaded file, so that i can feed it to a method that will return the values needed

Anyone know how i can accomplish this?

Thanks


Surely the name "InMemory" is a clue that the file exists in memory only, so doesn't have a path?


Daniel Roseman is correct for the context. But if you already created/inserted the object into the database using .create(...) or obj.save(). The path of the image can be retrieved by using obj.<field_name>.name. Example:

Suppose you have a model with an image field.

my_image = models.ImageFiled(upload_to='my_upload_dir')

so,

obj = ImageModel.objects.create(image=request.FILES['image']) # let's insert it to db
image_path = obj.image.name

# will return 
'my_upload_dir/myimagename.jpg'
0

精彩评论

暂无评论...
验证码 换一张
取 消