简单的注释代码无法与通知应用程序协作

时间:2019-06-04 15:46:47

标签: python django

我的注释代码可以正常工作,但是一旦我添加了通知功能,它就无法正常工作,而通知功能却可以工作。我正在使用django-notification-hq第三方应用程序。从这里开始:https://github.com/django-notifications/django-notifications

这是我的代码

class Comment(models.Model):
    post = models.ForeignKey(Post, on_delete=models.CASCADE)
    author = models.ForeignKey(settings.AUTH_USER_MODEL,  on_delete=models.CASCADE)
    message = models.TextField()
    ordered = models.BooleanField(default=False)

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ['-id']

    def get_edit_url(self):
        return reverse('comment_edit', args=[self.post.pk, self.pk])

    def get_delete_url(self):
        return reverse('comment_delete', args=[self.post.pk, self.pk])

    def save(self, *args, **kwargs):
        notify.send(self.author, recipient=self.post.author, action_object=self.post, target=self, verb="commented on")

views.py

@login_required
def comment_new(request, post_pk):
    post = get_object_or_404(Post, pk=post_pk)
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.post = post
            comment.author = request.user
            comment.save()
            return redirect(comment.post)
    else:
        form = CommentForm()
    return render(request, 'community/comment_form.html', {
        'form':form,
        })

urls.py

 path('post/<int:post_pk>/comment/new',views.comment_new, name='comment_new'),

我添加了这一行,

def save(self, *args, **kwargs):
            notify.send(self.author, recipient=self.post.author, action_object=self.post, target=self, verb="commented on")

我的新模特

class Comment(models.Model):
    post = models.ForeignKey(Post, on_delete=models.CASCADE)
    author = models.ForeignKey(settings.AUTH_USER_MODEL,  on_delete=models.CASCADE)
    message = models.TextField()
    ordered = models.BooleanField(default=False)

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ['-id']

    def get_edit_url(self):
        return reverse('comment_edit', args=[self.post.pk, self.pk])

    def get_delete_url(self):
        return reverse('comment_delete', args=[self.post.pk, self.pk])

        def save(self, *args, **kwargs):
            notify.send(self.author, 
                        recipient=self.post.author, 
                        action_object=self.post, 
                        target=self, 
                        verb="commented on")

        super(Comment, self).save(*args, **kwargs)

    error message: NameError: name 'Comment' is not defined

2 个答案:

答案 0 :(得分:1)

您已经在save处理程序中添加了自定义逻辑,但是没有进行super调用,您已经完全覆盖了基本save逻辑。

您的问题陈述太含糊,无法确定,但是我想在自定义super().save(*args, **kwargs)方法中添加save可以解决您遇到的任何问题。

答案 1 :(得分:1)

您正在覆盖模型上的保存方法,之后需要将参数传递给super()才能保持主要行为。下面的代码应使其像以前一样工作:

def save(self, *args, **kwargs):
    notify.send(self.author, 
                recipient=self.post.author, 
                action_object=self.post, 
                target=self, 
                verb="commented on")

    super(Class, self).save(*args, **kwargs)

尝试一下:

class Comment(models.Model):
    post = models.ForeignKey(Post, on_delete=models.CASCADE)
    author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    message = models.TextField()
    ordered = models.BooleanField(default=False)

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ['-id']

    def get_edit_url(self):
        return reverse('comment_edit', args=[self.post.pk, self.pk])

    def get_delete_url(self):
        return reverse('comment_delete', args=[self.post.pk, self.pk])

    def save(self, *args, **kwargs):
        notify.send(self.author, 
                    recipient=self.post.author, 
                    action_object=self.post, 
                    target=self, 
                    verb="commented on")

        super(Comment, self).save(*args, **kwargs)