简体   繁体   English

创建 object 时,如何基于另一个字段之一创建二维码?

[英]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:我想通过管理面板 Django 创建对象,我输入一个参数值,我想根据这个值生成一个二维码,我的代码:

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'此代码返回错误 [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我正在尝试使用信号@receive,但这对我没有帮助

Make sure that your created the mentioned directories ( img/qr_codes/ ).确保您创建了上述目录 ( 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.注意:我认为您在 f 弦后遗漏了右括号) Is that the same in your code?在你的代码中是一样的吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Django或JS开启相机并扫描二维码 - Using Django or JS to turn on camera and scan QR-code django模型中如何创建基于其他表的触发器更新字段 - How to create trigger update fields based on other table in django models 在Django中创建其他对象后如何自动创建新对象? - How create new object automatically after creating other object in django? 在创建新记录时如何根据其他字段更改字段read_only值? - How change fields read_only value based on other fields while creating new record? 创建对象时如何定义字段? - How do i define fields when creating an object? Django-使用其他对象模型根据预编译的字段打开创建/添加表单 - Django - Open create/add form with fields precompiled based on other object model 如何在Python中创建二维码而不另存为图片 - How do I create a QR code in Python without saving it as an image django模型,如何制作两个可选字段,但是填一个的时候,另一个也必须填 - Django models, How to make two optional fields, but when one is filled, the other one must also be filled 基于其他字段在Django中自动在用户模型中创建用户名 - Automatically create username in User model in Django based on other fields 在Django中创建数据库模型时,如何创建分别只接受一个月和一年的字段? - When creating a database model in Django, how can I create fields that will only accept a month and year, respectively?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM