my code for comment works fine but as soon as I add notification feature it doesn’t work, while notification kinda works. I’m using django-notification-hq 3rd party app. from here: https://github.com/django-notifications/django-notifications
here’s my code
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'),
I added this single line,
def save(self, *args, **kwargs):
notify.send(self.author, recipient=self.post.author, action_object=self.post, target=self, verb="commented on")
My new model
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
Advertisement
Answer
You are overwriting the save-method on the model, you need to pass the arguments to super() afterwards to maintain the primary behaviour. The below code should make it work as before:
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)
try this:
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)