django-filer icon indicating copy to clipboard operation
django-filer copied to clipboard

'File' object has no attribute '_committed'

Open techscientist opened this issue 9 years ago • 8 comments

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?

techscientist avatar Feb 16 '16 16:02 techscientist

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

joshuajonah avatar Feb 16 '16 20:02 joshuajonah

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 avatar Oct 18 '17 16:10 psychok7

@psychok7 how do I proceed when saving multiple files inside a FileField?

Olfredos6 avatar Sep 29 '18 08:09 Olfredos6

@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.

psychok7 avatar Sep 29 '18 12:09 psychok7

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

galilea-g avatar Oct 02 '19 21:10 galilea-g

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

HarshNarayanJha avatar Jul 13 '20 04:07 HarshNarayanJha

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.

stale[bot] avatar Jul 28 '22 18:07 stale[bot]

Abeg this is not solved yet

Jay-davisphem avatar Aug 01 '22 07:08 Jay-davisphem

This will now be closed due to inactivity, but feel free to reopen it.

stale[bot] avatar Aug 30 '22 17:08 stale[bot]