django-filer
django-filer copied to clipboard
'File' object has no attribute '_committed'
I am getting this error when trying to point an existing file to a FilerFileField of a model:
'File' object has no attribute '_committed'
`file, created = File.objects.get_or_create(owner=instance.issued_by, description=desc, name=filename, is_public=False, file=File(open(filename)))
l.info("Created %s file? %s" % (file.path, created))
instance.generated_doc = file
instance.save()
l.info("Done!")`
Can you please tell me how to programmatically save an existing file to the FilerFileField of the model?
I would start with posting all the code. Are you using filer.models.filemodels.File
?
See: https://github.com/divio/django-filer/blob/develop/filer/management/commands/import_files.py
Thanks, i was able to glue this together and make it work:
from django.core.files import File as DjangoFile
file_obj = DjangoFile(open(filename, mode='rb'), name=f'{instance.id}.csv')
obj, created = File.objects.get_or_create(
original_filename=file_obj.name, file=file_obj, folder=folder,
is_public=True, description='CSV'
)
instance.generated_csv = obj
instance.save()
@psychok7 how do I proceed when saving multiple files inside a FileField?
@Olfredos6 I don't think you can. I filefield only has 1 file. You can probably group everything under the same parent folder or use a many to many to achieve the same.
This work for me
from django.core.files import File as DjangoFile
file_obj = DjangoFile(open('C:/Users/test/Desktop/files/PDF.pdf', mode='rb'),name='PDF'])
arc = Test.objects.create(
files = file_obj,
)
Hi there, I had also similar error, but on the ImageField.
I tried to make my blogpost's modal thumbnail optional by typing null and blank True. Than i created a checkbox in forms.py and in the views.py I did the following
Views.py post = Post() if not form.cleaned_data["the_checkbox']: tumbnail = form.cleaned_data["thumbnail_file"] else: print("not a thumbnail")
#all other stuff here, username, subject etc if not form.cleaned_data["the_checkbox']: post.thumbnail = thumbnail
post.save()
#end Yes, I checked if the form.is_valid() and migrated my modals. Same worked for the profile picture, But in this, it is giving me error ImageField has no attribute "_commited"
The error arises from the post.save() line and in pre_save() method of modal
Please help me if there's anything that i don't know THANKS
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Abeg this is not solved yet
This will now be closed due to inactivity, but feel free to reopen it.