我有一个巨大的字符串,我想将其保存为Django模型中的文件。为此,我编写了以下代码:
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()
这会引发错误,
'_ io.TextIOWrapper'对象没有属性'_committed'
但在网上寻找解决方案后,我走到了尽头。任何建议将不胜感激!
环境:
请求方法:GET请求URL: http://127.0.0.1:8000/app/dbkarga/6/
Django版本:1.11.1 Python版本:3.6.1已安装的应用程序: ['django.contrib.admin','django.contrib.auth', 'django.contrib.contenttypes','django.contrib.sessions', 'django.contrib.messages','django.contrib.staticfiles', 'app.apps.AppConfig']已安装的中间件: [ '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']
回溯:
文件“\ venv \ lib \ site-packages \ django \ core \ handlers \ exception.py”in 内 41. response = get_response(request)
文件“\ venv \ lib \ site-packages \ django \ core \ handlers \ base.py”in _get_response 187. response = self.process_exception_by_middleware(e,request)
文件“\ venv \ lib \ site-packages \ django \ core \ handlers \ base.py”in _get_response 185. response = wrapped_callback(request,* callback_args,** callback_kwargs)
文件“\ venv \ lib \ site-packages \ django \ contrib \ auth \ decorators.py”in _wrapped_view 23. return view_func(request,* args,** kwargs)
dbkarga中的文件“\ venv \ project \ app \ views.py” 93. app.postgis2geojson.getData(gis_id,db_name,db_user,db_pass,db_addr,db_port,t)
getData中的文件“\ venv \ project \ app \ postgis2geojson.py” 119. my_obj.save()
保存文件“\ venv \ lib \ site-packages \ django \ db \ models \ base.py” 806. force_update = force_update,update_fields = update_fields)
save_base中的文件“\ venv \ lib \ site-packages \ django \ db \ models \ base.py” 836. updated = self._save_table(raw,cls,force_insert,force_update,using,update_fields)
_save_table中的文件“\ venv \ lib \ site-packages \ django \ db \ models \ base.py” 922. result = self._do_insert(cls._base_manager,using,fields,update_pk,raw)
_do_insert中的文件“\ venv \ lib \ site-packages \ django \ db \ models \ base.py” 961. using = using,raw = raw)
文件“\ venv \ lib \ site-packages \ django \ db \ models \ manager.py”中 manager_method 85. return getattr(self.get_queryset(),name)(* args,** kwargs)
文件“\ venv \ lib \ site-packages \ django \ db \ models \ query.py”中 _插入 1061. return query.get_compiler(using = using).execute_sql(return_id)
文件“\ venv \ lib \ site-packages \ django \ db \ models \ sql \ compiler.py”in execute_sql 1098.对于sql,self.as_sql()中的params:
文件“\ venv \ lib \ site-packages \ django \ db \ models \ sql \ compiler.py”in as_sql 1051.对于obj in self.query.objs
文件“\ venv \ lib \ site-packages \ django \ db \ models \ sql \ compiler.py”in 1051.对于obj in self.query.objs
文件“\ 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 field]
文件“\ venv \ lib \ site-packages \ django \ db \ models \ sql \ compiler.py”in pre_save_val 1000. return field.pre_save(obj,add = True)
文件“\ venv \ lib \ site-packages \ django \ db \ models \ fields \ files.py”in pre_save 295.如果是file而不是file._committed:
异常类型:/ app / dbkarga / 6 /异常值的AttributeError: '_io.TextIOWrapper'对象没有'_committed'属性
答案 0 :(得分:0)
好的,我现在看到你的问题了。
Outfile必须是django.core.files.File
或django.core.files.base.ContentFile
的实例(有关详细信息,请参阅手册here)。
您可以使用的两个选择是:
# 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'))
更新了如何阅读文件
f = Model_Type.objects.all().get(id=0).my_file
f.open(mode='rb')
lines = f.readlines()
f.close()