I am using Wagtail + Django_comments_xtd + Django.
My_Django_App/models.py
from wagtail.core.models import Page
class PostPage(Page):
...
from django_comments_xtd.models import XtdComment
class PostComment(XtdComment):
page = ParentalKey('PostPage', on_delete=models.CASCADE, related_name='rn_comments')
def save(self, *args, **kwargs):
if self.user:
self.user_name = self.user.username
self.page = PostDetail.objects.get(pk=self.object_pk)
super(PostComment, self).save(*args, **kwargs)
On Wagtail CMS, if I revise an existing post which has some comments already and publish the revised post again, I thought PostComment.save()
should not be triggered any more. However, during my debug, I found it was triggered unexpectedly.
I guess that I need to fine-tune PostComment.save()
to achieve above intention.
After some researches on StackOverflow,
- Identifying new Model Instance in Django Save with UUID pk
- In a django model custom save() method, how should you identify a new object?
I realize that I might need to use PostComment._state.adding
and force_insert
within the save()
to achieve my intention.
Can anyone show me how should I fine-tune PostComment.save()
?