title: Creating file and saving in Django model (AttributeError?)
body: <p>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:</p>
  
  <pre><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()
  </code></pre>
  
  <p>This raises an error, </p>
  
  <blockquote>
    <p>'_io.TextIOWrapper' object has no attribute '_committed' </p>
  </blockquote>
  
  <p>But after searching online for solutions, I've come to a dead end. Any advice will be gratefully appreciated!</p>
  
  <blockquote>
    <p>Environment:</p>
    
    <p>Request Method: GET Request URL:
    <a href="http://127.0.0.1:8000/app/dbkarga/6/" rel="nofollow noreferrer">http://127.0.0.1:8000/app/dbkarga/6/</a></p>
    
    <p>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']</p>
    
    <p>Traceback:</p>
    
    <p>File "\venv\lib\site-packages\django\core\handlers\exception.py" in
    inner
      41.             response = get_response(request)</p>
    
    <p>File "\venv\lib\site-packages\django\core\handlers\base.py" in
    _get_response
      187.                 response = self.process_exception_by_middleware(e, request)</p>
    
    <p>File "\venv\lib\site-packages\django\core\handlers\base.py" in
    _get_response
      185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)</p>
    
    <p>File "\venv\lib\site-packages\django\contrib\auth\decorators.py" in
    _wrapped_view
      23.                 return view_func(request, *args, **kwargs)</p>
    
    <p>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)</p>
    
    <p>File "\venv\project\app\postgis2geojson.py" in getData
      119.    my_obj.save()</p>
    
    <p>File "\venv\lib\site-packages\django\db\models\base.py" in save
      806.                        force_update=force_update, update_fields=update_fields)</p>
    
    <p>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)</p>
    
    <p>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)</p>
    
    <p>File "\venv\lib\site-packages\django\db\models\base.py" in _do_insert
      961.                                using=using, raw=raw)</p>
    
    <p>File "\venv\lib\site-packages\django\db\models\manager.py" in
    manager_method
      85.                 return getattr(self.get_queryset(), name)(*args, **kwargs)</p>
    
    <p>File "\venv\lib\site-packages\django\db\models\query.py" in
    _insert
      1061.         return query.get_compiler(using=using).execute_sql(return_id)</p>
    
    <p>File "\venv\lib\site-packages\django\db\models\sql\compiler.py" in
    execute_sql
      1098.             for sql, params in self.as_sql():</p>
    
    <p>File "\venv\lib\site-packages\django\db\models\sql\compiler.py" in
    as_sql
      1051.                 for obj in self.query.objs</p>
    
    <p>File "\venv\lib\site-packages\django\db\models\sql\compiler.py" in
    
      1051.                 for obj in self.query.objs</p>
    
    <p>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]</p>
    
    <p>File "\venv\lib\site-packages\django\db\models\sql\compiler.py" in
    pre_save_val
      1000.         return field.pre_save(obj, add=True)</p>
    
    <p>File "\venv\lib\site-packages\django\db\models\fields\files.py" in
    pre_save
      295.         if file and not file._committed:</p>
    
    <p>Exception Type: AttributeError at /app/dbkarga/6/ Exception Value:
    '_io.TextIOWrapper' object has no attribute '_committed'</p>
  </blockquote>