Created
October 24, 2020 14:04
-
-
Save godfather68/791d1b8fb69879d42ac115ca9f2823b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
import barcode # additional imports | |
from barcode.writer import ImageWriter | |
from io import BytesIO | |
from django.core.files import File | |
# Create your models here. | |
class Customer(models.Model): | |
name = models.CharField(max_length=50) | |
barcode = models.ImageField(upload_to='barcodes/', blank=True) | |
def __str__(self): | |
return self.name | |
def save(self, *args, **kwargs): # overriding save() | |
COD128 = barcode.get_barcode_class('code128') | |
rv = BytesIO() | |
code = COD128(f'{self.name}', writer=ImageWriter()).write(rv) | |
self.barcode.save(f'{self.name}.png', File(rv), save=False) | |
return super().save(*args, **kwargs) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment