[Answered ]-Creating file and saving in Django model (AttributeError?)

2👍

Okay I see your problem now.

Outfile must be an instance of either django.core.files.File or django.core.files.base.ContentFile (see manual here for details).

The two choices you can use are:

# Using File
outfile = open('/path/to/file')
my_obj = Model_Type(obj_name = name, my_file = File(outfile))
# Using ContentFile
my_obj = Model_Type(obj_name = name, my_file = ContentFile('Your very long string goes here'))

Updated with how to read the file

f = Model_Type.objects.all().get(id=0).my_file 
f.open(mode='rb') 
lines = f.readlines()
f.close()
👤Colwin

Leave a comment