There is a model Model in the model data field of type FileFiled. Text files are stored.
I get the model:

model = Model.objects.get(id=1) 

Then take out the file

 file = model.data 

Tell me how to add information to it.
The type of the variable file is "django.db.models.fields.files.FieldFile". If you use the dir method, the write method is. But when I try to write

 file.write('some string') 

gives an error message:

io.UnsupportedOperation: write

Tried different options using open

 file.open(mode='w') 

what if

 temp_file = open(file, mode='w') temp_file.write('some string') # тут ошибка TypeError: invalid file: 

I tried various mods.
Tried to refer to the FieldFile.file field.

 file = model.data.file 

I get the object 'django.core.files.base.File' which also has a write method, but the same story with it.
Tell me how you can add (add) to the file the necessary lines.

    1 answer 1

    Try this.

     from django.core.files import File model = Model.objects.get(id=1) with open(model.data.path, 'w') as f: myfile = File(f) myfile.write('qwerty') 
    • Thank. It worked as it should. The only mode had to put 'ab'. open (model.data.path, 'ab'). - Vetos