__init __()缺少1个必需的位置参数:“保存”

时间:2019-08-06 15:20:56

标签: python django python-3.x django-models init

尝试为每个实例创建一个QR代码生成器,但出现以下错误: init ()缺少1个必需的位置参数:“保存”

有人可以告诉我我的代码有问题吗,或者是一种更好的方法来为每个模型生成QR代码?

我也尝试将qr代码生成也包括在方法中。

from django.db import models
import qrcode

# Create your models here.
class Attendee(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    present = models.BooleanField(default=False)
    qr = qrcode.QRCode(
        version=1,
        box_size=15,
        border=5
        )
    data = 'http://localhost:8000/attendees/confirmation/'
    qr.add_data(data)
    qr.make(fit=True)
    img = qr.make_image(fill='black', back_color='white')
    s = img.save('C:/Users/Storm/Projects/QRAttendance/attendees/static/images/QR1.png')
    # save = img.save('QR_'+str(first_name)+'_'+str(last_name)+'.png')

    def __init__(self, data, img, qrcode, qr, s):
        self.s = s

    def __str__(self):
        fullname = '{0.last_name}, {0.first_name}, {0.present}'
        return fullname.format(self)

当前错误是:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Users\Storm\Envs\QRAttendance\lib\site-packages\django\db\models\query.py", line 250, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "C:\Users\Storm\Envs\QRAttendance\lib\site-packages\django\db\models\query.py", line 274, in __iter__
    self._fetch_all()
  File "C:\Users\Storm\Envs\QRAttendance\lib\site-packages\django\db\models\query.py", line 1242, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\Users\Storm\Envs\QRAttendance\lib\site-packages\django\db\models\query.py", line 73, in __iter__
    obj = model_cls.from_db(db, init_list, row[model_fields_start:model_fields_end])
  File "C:\Users\Storm\Envs\QRAttendance\lib\site-packages\django\db\models\base.py", line 513, in from_db
    new = cls(*values)
TypeError: __init__() missing 1 required positional argument: 's'

0 个答案:

没有答案