1
votes

I have a huge string that I want to save as a file in a Django model. In order to do that, I have written the following code:

with open("new_file", 'w') as outfile:
    outfile.write(myString)
    outfile.close()


my_obj = Model_Type(obj_name = name, my_file = outfile)
my_obj.save()

This raises an error,

'_io.TextIOWrapper' object has no attribute '_committed'

But after searching online for solutions, I've come to a dead end. Any advice will be gratefully appreciated!

Environment:

Request Method: GET Request URL: http://127.0.0.1:8000/app/dbkarga/6/

Django Version: 1.11.1 Python Version: 3.6.1 Installed Applications: ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'app.apps.AppConfig'] Installed Middleware: ['django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware']

Traceback:

File "\venv\lib\site-packages\django\core\handlers\exception.py" in inner 41. response = get_response(request)

File "\venv\lib\site-packages\django\core\handlers\base.py" in _get_response 187. response = self.process_exception_by_middleware(e, request)

File "\venv\lib\site-packages\django\core\handlers\base.py" in _get_response 185. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "\venv\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view 23. return view_func(request, *args, **kwargs)

File "\venv\project\app\views.py" in dbkarga 93. app.postgis2geojson.getData(gis_id, db_name, db_user, db_pass, db_addr, db_port, t)

File "\venv\project\app\postgis2geojson.py" in getData 119. my_obj.save()

File "\venv\lib\site-packages\django\db\models\base.py" in save 806. force_update=force_update, update_fields=update_fields)

File "\venv\lib\site-packages\django\db\models\base.py" in save_base 836. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)

File "\venv\lib\site-packages\django\db\models\base.py" in _save_table 922. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)

File "\venv\lib\site-packages\django\db\models\base.py" in _do_insert 961. using=using, raw=raw)

File "\venv\lib\site-packages\django\db\models\manager.py" in manager_method 85. return getattr(self.get_queryset(), name)(*args, **kwargs)

File "\venv\lib\site-packages\django\db\models\query.py" in _insert 1061. return query.get_compiler(using=using).execute_sql(return_id)

File "\venv\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql 1098. for sql, params in self.as_sql():

File "\venv\lib\site-packages\django\db\models\sql\compiler.py" in as_sql 1051. for obj in self.query.objs

File "\venv\lib\site-packages\django\db\models\sql\compiler.py" in 1051. for obj in self.query.objs

File "\venv\lib\site-packages\django\db\models\sql\compiler.py" in 1050. [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]

File "\venv\lib\site-packages\django\db\models\sql\compiler.py" in pre_save_val 1000. return field.pre_save(obj, add=True)

File "\venv\lib\site-packages\django\db\models\fields\files.py" in pre_save 295. if file and not file._committed:

Exception Type: AttributeError at /app/dbkarga/6/ Exception Value: '_io.TextIOWrapper' object has no attribute '_committed'

1
Can you please post the entire traceback of the exception?Colwin

1 Answers

2
votes

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()