Install Django FileField in an Existing File

I have an existing file on disk (say / folder / file.txt) and a FileField model field in Django.

When i do

instance.field = File(file('/folder/file.txt')) instance.save() 

it re-saves the file as file_1.txt (next time it is _2 , etc.).

I understand why, but I do not want this behavior. I know that the file I want to associate the field with is really waiting for me, and I just want Django to point to it.

How?

+61
python file django
Nov 30 '11 at 20:20
source share
5 answers

If you want to do this forever, you need to create your own FileStorage class.

 from django.core.files.storage import FileSystemStorage class MyFileStorage(FileSystemStorage): # This method is actually defined in Storage def get_available_name(self, name): return name # simply returns the name passed 

Now in your model you are using a modified MyFileStorage

 from mystuff.customs import MyFileStorage mfs = MyFileStorage() class SomeModel(model.Model): my_file = model.FileField(storage=mfs) 
+18
Dec 01 '11 at 6:14
source share

just set instance.field.name to the path of your file

eg.

 class Document(models.Model): file = FileField(upload_to=get_document_path) description = CharField(max_length=100) doc = Document() doc.file.name = 'path/to/file' # must be relative to MEDIA_ROOT doc.file <FieldFile: path/to/file> 
+81
Jun 05 2018-12-12T00:
source share

try this ( doc ):

 instance.field.name = <PATH RELATIVE TO MEDIA_ROOT> instance.save() 
+7
Aug 23 '13 at 15:08
source share

Write your own storage class correctly. However, get_available_name not suitable for overriding.

get_available_name is called when Django sees a file with the same name and tries to get a new available file name. This is not a method that causes renaming. the method called this _save . The comments in _save pretty good, and you can easily find that it opens a file for writing with the os.O_EXCL flag, which throws OSError if the same file name already exists. Django catches this error, then calls get_available_name to get the new name.

So, I think the correct way is to override _save and call os.open () without the os.O_EXCL flag. The modification is quite simple, but the method is a bit long, so I do not insert it here. Tell me if you need more help :)

+4
Mar 23 2018-12-12T00:
source share

I had the same problem! then I understand that my Models caused this. For example, I had such models:

 class Tile(models.Model): image = models.ImageField() 

Then I wanted to have more than one tile referencing the same file on disk! The way I decided to solve this was to change my model structure:

 class Tile(models.Model): image = models.ForeignKey(TileImage) class TileImage(models.Model): image = models.ImageField() 

That, after I realized that it makes more sense, because if I want the same file to be saved more than one in my database, I need to create another table for it!

I think you can solve your problem too, just hoping that you can change the models!

EDIT

I also assume that you can use another repository, for example this: SymlinkOrCopyStorage

http://code.welldev.org/django-storages/src/11bef0c2a410/storages/backends/symlinkorcopy.py

0
Nov 30 '11 at 20:51
source share



All Articles