Django / Python - change does not save in database - AttributeError: 'QuerySet' object has no attribute 'reload'

Django / Python - change does not save in database - AttributeError: 'QuerySet' object has no attribute 'reload'

我有一个用户可以更改的设置:自动或手动发送电子邮件。为此,我创建了一个只有 1 行的数据库,其中包含以下列:

class AutoSendMail(models.Model):
    auto = models.BooleanField(default=False)
    manual = models.BooleanField(default=True)
    send_type = (
        ('manual', 'MANUAL'),
        ('auto', 'AUTO')
    )
    type = models.CharField(max_length=6, choices=send_type, default="manual")

    def get(self):
        new_self = self.__class__.objects.get(pk=self.pk)
        # You may want to clear out the old dict first or perform a selective merge
        self.__dict__.update(new_self.__dict__)
        return reverse("core:autosend", kwargs={"auto": self.auto})

    def reload(self):
        new_self = self.__class__.objects.get(pk=self.pk)
        # You may want to clear out the old dict first or perform a selective merge
        self.__dict__.update(new_self.__dict__)

这其中,'auto'和'manual'其中一个为True,一个为False。 'type' 相应地设置为 'auto' 或 'manual'。此设置用于代码的其余部分。我现在认为的代码是:

class AutoSendView(generic.TemplateView):

    template_name = 'core/mailbox/autoSendMail.html'
    context_object_name = 'autosend'
    extra_context = {"mailbox_page": "active"}

    model = AutoSendMail.objects.get(id=1)
    model.refresh_from_db()

    autoSetting = int(model.auto == True)
    manualSetting = int(model.manual == True)

    def post(self, request, *args, **kwargs):
        id_ = self.kwargs.get("pk")
        update_type = self.request.POST.get('update_type')

        if update_type == 'manual':
            logger.info("Set to: manual email send")
            model = AutoSendMail.objects.filter(id=1)
            model.manual = True
            model.auto = False
            model.type = "manual"
            for object in model:
                object.save()
                model.reload()

            return redirect("core:autosend")

        elif update_type == 'auto':
            logger.info("Set to: auto email send")
            model = AutoSendMail.objects.filter(id=1)
            model.manual = False
            model.auto = True

            model.type = "auto"
            for object in model:
                object.save()
                model.reload()

            return redirect("core:autosend")

我的问题是用户在设置中所做的更改没有保存在数据库 (sqlite3) 中。我不确定为什么不。因为当我在我的 python 控制台中尝试这个时,它确实有效。所以我做错了。

使用上面的代码我得到一条错误消息:

ERROR Internal Server Error: /mailbox/autosend
Traceback (most recent call last):
  File "C:\Users\Portal\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Portal\venv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Portal\venv\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "C:\Users\Portal\venv\lib\site-packages\django\views\generic\base.py", line 69, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\Portal\venv\lib\site-packages\django\views\generic\base.py", line 101, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Users\portal\core\views.py", line 200, in post
    model.reload()
AttributeError: 'QuerySet' object has no attribute 'reload'
ERROR "POST /mailbox/autosend HTTP/1.1" 500 2657

有人知道如何处理这个问题吗?当我删除 'reload' 时,它给出错误 'QuerySet' object has no attribute 'save'。如果我也删除 'save()',它不会保存更改

filter(...) will return a Queryset, but get(...) returns 对象。所以替换:

model = AutoSendMail.objects.filter(id=1)

model = AutoSendMail.objects.get(id=1)

候补

我认为您的代码非常冗余且未优化。我想你可以使用下面的代码:

if update_type == 'auto':
    model = AutoSendMail.objects.filter(id=1).update(type="auto", auto=True)