简体   繁体   中英

how to create a qr-code an sone of one of the fields based on the other when creating an object?

I want to creato objects through admin-pannel Django, I enter a value for a parameter and I want a qr code to be generated based on this value, my code:

class People(models.Model):
    name = models.CharField(max_length=500, unique=True)
    qr_code = models.ImageField(upload_to="img/qr_codes/", verbose_name="QR-code", null = True)

    def save(self, *args, **kwargs):
        qr = qrcode.QRCode(version=2, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=1)
        qr.add_data(self.name)
        qr.make(fit=True)
        qr.make_image().save(f'img/qr_codes/{self.name}.png')
        self.qr_code = self.name+'.png'
        super().save(*args, **kwargs)

This code return error [Errno 2] No such file or directory: 'img/qr_codes/somename.png'

Im trying to use signal @receive but it isn't help for me

Make sure that your created the mentioned directories ( img/qr_codes/ ). the method only creates file. It cannot create a directory.

Note: I think you missed a closing bracket ) after your f-string. Is that the same in your code?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM