question_on_approve() 缺少 1 个必需的位置参数:'created'

question_on_approve() missing 1 required positional argument: 'created'

一旦问题发布在模型上,我将尝试加分。但它显示 question_on_approve() 缺少 1 个必需的位置参数:'created'.

@receiver(pre_save, sender=question)
def question_on_approve(sender, instance, created, **kwargs):
    if not created:
        oldQuestionObj = question.object.get(pk=instance.pk)
        questionObj = instance
        if oldQuestionObj.status != 1 and questionObj.status == 1:
            Points.objects.create(point=somepointfromPointsTableForQuestionAsked)

created 仅适用于 post_save,因为此时不知道对象是新记录还是现有记录。如果对象尚未创建,这通常意味着 instance.pk 尚未设置,因此您可以使用:

@receiver(pre_save, sender=question)
def question_on_approve(sender, instance, **kwargs):
#                            no created ↑
    if <strong>instance.pk is not None</strong>:
        oldQuestionObj = question.object.get(pk=instance.pk)
        questionObj = instance
        if oldQuestionObj.status != 1 and questionObj.status == 1:
            Points.objects.create(point=somepointfromPointsTableForQuestionAsked)