gpt4 book ai didi

python -/accounts/sign_up/create() 处的 TypeError 需要 1 个位置参数,但给出了 2 个

转载 作者:行者123 更新时间:2023-12-01 08:08:00 25 4
gpt4 key购买 nike

当我尝试创建新用户时,它会显示此错误

return getattr(self.get_queryset(), name)(*args, **kwargs)
TypeError: create() takes 1 positional argument but 2 were given

这是我的模型

class Notification(models.Model):
title = models.CharField(max_length=250)
message = models.TextField()
viewed = models.BooleanField(default=False)
user = models.ForeignKey(User, on_delete=models.DO_NOTHING)


def create_welcome_message(sender, **kwargs):
if kwargs['created']:
noti=Notification.objects.create(kwargs['instance'],
title="Welcome Message",
message="Thank you for singing up!")


post_save.connect(create_welcome_message, sender=User)

我已经失踪很久了。使用这种语言。然后帮我解决这个错误

最佳答案

即使对于 kwargs['instance'],您也需要将关键字参数传递给 create() 方法,例如:

.create(instance=kwargs['instance'])

或者您可以使用 add() 方法代替:

Notification.objects.add(kwargs['instance'])

更好的通知替代方案:

pip install django-notifications-hq

from django.db.models.signals import post_save
from notifications.signals import notify
from myapp.models import MyModel

def my_handler(sender, instance, created, **kwargs):
notify.send(instance, verb='was saved')

post_save.connect(my_handler, sender=MyModel)

关于python -/accounts/sign_up/create() 处的 TypeError 需要 1 个位置参数,但给出了 2 个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55440246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com