Storing ZipFile Objects into the Django database

前端 未结 1 1347
遥遥无期
遥遥无期 2021-01-22 05:52

The problem I have is quite uncommon I think, because I didn\'t seem to be able to find an answer on here or on Google.
I have several pictures stored in my database and in

相关标签:
1条回答
  • 2021-01-22 06:11

    This was kind of not as complicated as I thought. (Which could explain why no one asked that question all over the internet I guess)
    Using Python 3.3, your strings are unicode and you mainly work with unicode objects. File needs bytes data to work correctly so here is the solution :

    zipname = "{}.zip".format(reporting.id, reporting.title)
    
    with ZipFile(zipname, 'w') as zf:
        # Generating the ZIP ! 
    
    reporting = Reporting.objects.get(pk=reporting_id)
    reporting.pictures_archive.delete()
    reporting.pictures_archive = File(open(zipname, "rb"))
    reporting.save()
    
    0 讨论(0)
提交回复
热议问题