'Unsupported operation error creating and saving json file
Ive been trying to save a json file created from a to a filefield, I got an unsuported operation error "not readable", this is my code
from django.core.files.base import File
@receiver(post_save,sender=ProyArq)
def ifc_a_json(sender,instance,*args,**kwargs):
if instance.arch_ifc:
jsoon = ifc_2_json("path_to_file")
json_file_nom = instance.nombre.replace(' ','')+'.json'
with open(json_file_nom, 'w') as outfile:
json.dump(jsoon, outfile, indent=2)
json_fk = JsonIFC.objects.create(proy_fk=instance)
json_fk.ifc_json.save(json_file_nom,File(outfile),True)
im working with IFC files, and I want to store them also as json, I tried instead of saving the json as a JSONField, to save it as a foreign key file since the size of the json Im working are above 10mb, is this the best approach for this??
error details
Exception Type: UnsupportedOperation
Exception Value: not readable
Exception Location: /home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/core/files/base.py in chunks, line 60
Traceback (most recent call last):
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/contrib/admin/options.py", line 607, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/contrib/admin/sites.py", line 231, in inner
return view(request, *args, **kwargs)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/contrib/admin/options.py", line 1641, in change_view
return self.changeform_view(request, object_id, form_url, extra_context)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/utils/decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/contrib/admin/options.py", line 1522, in changeform_view
return self._changeform_view(request, object_id, form_url, extra_context)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/contrib/admin/options.py", line 1565, in _changeform_view
self.save_model(request, new_object, form, not add)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/contrib/admin/options.py", line 1081, in save_model
obj.save()
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/db/models/base.py", line 746, in save
force_update=force_update, update_fields=update_fields)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/db/models/base.py", line 795, in save_base
update_fields=update_fields, raw=raw, using=using,
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 175, in send
for receiver in self._live_receivers(sender)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 175, in <listcomp>
for receiver in self._live_receivers(sender)
File "/home/fcr/anaconda3/envs/gda/a_viurb/b_proy/models.py", line 105, in ifc_a_json
json_fk.ifc_json.save(json_file_nom,File(outfile),True)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/db/models/fields/files.py", line 87, in save
self.name = self.storage.save(name, content, max_length=self.field.max_length)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/core/files/storage.py", line 52, in save
return self._save(name, content)
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/core/files/storage.py", line 271, in _save
for chunk in content.chunks():
File "/home/fcr/anaconda3/envs/gda/lib/python3.7/site-packages/django/core/files/base.py", line 60, in chunks
data = self.read(chunk_size)
io.UnsupportedOperation: not readable
Solution 1:[1]
I solved the error, I needed to change the file open from "w" to "a+", which allows reading and writing
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | zevloo |
