Django 生成条形码并保存到图像字段中

Django generate bar code and save into image field

我能够生成条形码,并且可以使用此库将图像文件保存在根文件夹中 python-barcode。 现在我正在尝试生成条形码图像并保存到 django 图像文件中。

选拔赛:

import barcode

bc = Barcode.objects.latest('id')

upc = barcode.get('upc', '123456789102', writer=ImageWriter())
img = upc.render()  # Returns PIL image class
# <PIL.Image.Image image mode=RGB size=523x280 at 0x7FAE2B471320>
bc.img = img
bc.save()

获取错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-77-4ff34d9ac4c9> in <module>
----> 1 bc.save()

~/Desktop/workspace/projects/barcodescan/scan/models.py in save(self, *args, **kwargs)
     19         code = get_random_string(length=11, allowed_chars='1234567890')
     20         self.code = str(code)
---> 21         super(BarCode, self).save(*args, **kwargs)
     22 
     23     def __str__(self):

~/Desktop/workspace/projects/barcodescan/venv/lib/python3.5/site-packages/django/db/models/base.py in save(self, force_insert, force_update, using, update_fields)
    739 
    740         self.save_base(using=using, force_insert=force_insert,
--> 741                        force_update=force_update, update_fields=update_fields)
    742     save.alters_data = True
    743 

~/Desktop/workspace/projects/barcodescan/venv/lib/python3.5/site-packages/django/db/models/base.py in save_base(self, raw, force_insert, force_update, using, update_fields)
    777             updated = self._save_table(
    778                 raw, cls, force_insert or parent_inserted,
--> 779                 force_update, using, update_fields,
    780             )
    781         # Store the database on which the object was saved

~/Desktop/workspace/projects/barcodescan/venv/lib/python3.5/site-packages/django/db/models/base.py in _save_table(self, raw, cls, force_insert, force_update, using, update_fields)
    846             base_qs = cls._base_manager.using(using)
    847             values = [(f, None, (getattr(self, f.attname) if raw else f.pre_save(self, False)))
--> 848                       for f in non_pks]
    849             forced_update = update_fields or force_update
    850             updated = self._do_update(base_qs, using, pk_val, values, update_fields,

~/Desktop/workspace/projects/barcodescan/venv/lib/python3.5/site-packages/django/db/models/base.py in <listcomp>(.0)
    846             base_qs = cls._base_manager.using(using)
    847             values = [(f, None, (getattr(self, f.attname) if raw else f.pre_save(self, False)))
--> 848                       for f in non_pks]
    849             forced_update = update_fields or force_update
    850             updated = self._do_update(base_qs, using, pk_val, values, update_fields,

~/Desktop/workspace/projects/barcodescan/venv/lib/python3.5/site-packages/django/db/models/fields/files.py in pre_save(self, model_instance, add)
    284     def pre_save(self, model_instance, add):
    285         file = super().pre_save(model_instance, add)
--> 286         if file and not file._committed:
    287             # Commit the file to storage prior to saving the model
    288             file.save(file.name, file.file, save=False)

AttributeError: 'Image' object has no attribute '_committed'

我找不到解决这个问题的方法,我请求你给我一些建议来解决这个问题。它会很感激我的。提前致谢

PIL 图像对象到 Django 图像字段

经过一些研究,我找到了我的问题的解决方案!

from io import BytesIO 

import barcode
from barcode.writer import ImageWriter

from django.core.files.base import ContentFile

from scan.models import Barcode

bc = Barcode.obejcts.latest('id')

upc = barcode.get('upc', bc.code, writer=ImageWriter())

i = upc.render() # <PIL.Image.Image image mode=RGB size=523x280 at 0x7FAE2B471320>

image_io = BytesIO()

i.save(image_io, format='PNG')

image_name = 'test.png'

bc.img.save(image_name, content=ContentFile(image_io.getvalue()), save=False)

bc.save()