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
JavaScript
x
21
21
1
class Comment(models.Model):
2
post = models.ForeignKey(Post, on_delete=models.CASCADE)
3
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
4
message = models.TextField()
5
ordered = models.BooleanField(default=False)
6
7
created_at = models.DateTimeField(auto_now_add=True)
8
updated_at = models.DateTimeField(auto_now=True)
9
10
class Meta:
11
ordering = ['-id']
12
13
def get_edit_url(self):
14
return reverse('comment_edit', args=[self.post.pk, self.pk])
15
16
def get_delete_url(self):
17
return reverse('comment_delete', args=[self.post.pk, self.pk])
18
19
def save(self, *args, **kwargs):
20
notify.send(self.author, recipient=self.post.author, action_object=self.post, target=self, verb="commented on")
21
views.py
JavaScript
1
17
17
1
@login_required
2
def comment_new(request, post_pk):
3
post = get_object_or_404(Post, pk=post_pk)
4
if request.method == 'POST':
5
form = CommentForm(request.POST)
6
if form.is_valid():
7
comment = form.save(commit=False)
8
comment.post = post
9
comment.author = request.user
10
comment.save()
11
return redirect(comment.post)
12
else:
13
form = CommentForm()
14
return render(request, 'community/comment_form.html', {
15
'form':form,
16
})
17
urls.py
JavaScript
1
2
1
path('post/<int:post_pk>/comment/new',views.comment_new, name='comment_new'),
2
I added this single line,
JavaScript
1
3
1
def save(self, *args, **kwargs):
2
notify.send(self.author, recipient=self.post.author, action_object=self.post, target=self, verb="commented on")
3
My new model
JavaScript
1
29
29
1
class Comment(models.Model):
2
post = models.ForeignKey(Post, on_delete=models.CASCADE)
3
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
4
message = models.TextField()
5
ordered = models.BooleanField(default=False)
6
7
created_at = models.DateTimeField(auto_now_add=True)
8
updated_at = models.DateTimeField(auto_now=True)
9
10
class Meta:
11
ordering = ['-id']
12
13
def get_edit_url(self):
14
return reverse('comment_edit', args=[self.post.pk, self.pk])
15
16
def get_delete_url(self):
17
return reverse('comment_delete', args=[self.post.pk, self.pk])
18
19
def save(self, *args, **kwargs):
20
notify.send(self.author,
21
recipient=self.post.author,
22
action_object=self.post,
23
target=self,
24
verb="commented on")
25
26
super(Comment, self).save(*args, **kwargs)
27
28
error message: NameError: name 'Comment' is not defined
29
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:
JavaScript
1
9
1
def save(self, *args, **kwargs):
2
notify.send(self.author,
3
recipient=self.post.author,
4
action_object=self.post,
5
target=self,
6
verb="commented on")
7
8
super(Class, self).save(*args, **kwargs)
9
try this:
JavaScript
1
27
27
1
class Comment(models.Model):
2
post = models.ForeignKey(Post, on_delete=models.CASCADE)
3
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
4
message = models.TextField()
5
ordered = models.BooleanField(default=False)
6
7
created_at = models.DateTimeField(auto_now_add=True)
8
updated_at = models.DateTimeField(auto_now=True)
9
10
class Meta:
11
ordering = ['-id']
12
13
def get_edit_url(self):
14
return reverse('comment_edit', args=[self.post.pk, self.pk])
15
16
def get_delete_url(self):
17
return reverse('comment_delete', args=[self.post.pk, self.pk])
18
19
def save(self, *args, **kwargs):
20
notify.send(self.author,
21
recipient=self.post.author,
22
action_object=self.post,
23
target=self,
24
verb="commented on")
25
26
super(Comment, self).save(*args, **kwargs)
27