17👍
I don’t think you need to use raw SQL for this. I think you need to rename the file using the os
facility, then set the model’s FileField
name to the new name. Maybe something like:
os.rename(model.direct_file.path, new_path)
model.direct_file.name = new_name
model.save()
5👍
new_name = 'photos_preview/' + str(uuid.uuid1())
os.rename(photo.image_preview.path, settings.MEDIA_ROOT + new_name)
photo.image_preview.name = new_name
photo.save()
- In Django how do I notify a parent when a child is saved in a foreign key relationship?
- How to install gnu gettext (>0.15) on windows? So I can produce .po/.mo files in Django
- Class based generic views extra context
- Django/Celery multiple queues on localhost – routing not working
- Running collectstatic on server : AttributeError: 'PosixPath' object has no attribute 'startswith'
3👍
The current Django documentation states:
“When you access a FileField on a model, you are given an instance of FieldFile as a proxy for accessing the underlying file.” See docs for further reading.
Instead of using the Python File object to open the file, you should use FieldFile.open() to open the file, then manipulate the file’s path accordingly. Afterward, save the model object, and the changes to the path should persist.
1👍
I came across this issue when I had blobs saved into django with no file extension, and I wanted to correct that. Best used when looping over a filtered queryset.
You cannot change instance.picture.path, and trying to access instance.picture.file.* will give an error because accessing it will try to open the old file. Setting instance.picture.name will still not let you access instance.picture.file.*, even after saving.
You can simply set the ImageField object itself to the location and all will work:
(Tested with django 1.10)
import imghdr
import os
from django.db import models
class MyModel(models.Model):
picture = models.ImageField()
instance = MyModel.objects.first()
if os.path.exists(instance.picture.path):
extension = imghdr.what(instance.picture.path)
os.rename(instance.picture.path, instance.picture.path + '.' + extension)
instance.picture = instance.picture.name + '.' + extension
instance.save()
- Getting 415 code with django.test.Client's patch method
- How to Paginate within an action in Django Rest Framework
- How to add button to "submit_row" context in django
- Django model save – override method not invoked during migrations
1👍
You may use the following:
Suppose ‘obj’ is the django object that you want to rename. Then do this:
obj.file_field_name.name = new_name
obj.save()
- How to make GET CORS request with authorization header
- Django-compressor not setting absolute CSS image paths on Heroku
- Missing libgeos_c.so on OSX
0👍
It seems changing filename of a binary in FileField is pretty unflexible according to the django docs. It contains the path from Media root.
That points out to a name attr that is reflecting the path, not just the filename itself. Docs: This is the way that django model can find the file
- Removing the Label From Django's TextArea Widget
- Will Django be a good choice for a permissions based web-app?
- How to change my django server time
- How to create new resource with foreign key in TastyPie
- Should south migration files be added to source control?