danirus / django-comments-xtd

A pluggable Django comments application with thread support, follow-up notifications, mail confirmation, like/dislike flags, moderation, a ReactJS plugin and Bootstrap 5.3.
https://django-comments-xtd.readthedocs.io
BSD 2-Clause "Simplified" License
595 stars 158 forks source link

`id` to replace `pk` for `is_new = self.pk is None` in XtdComment.save() #353

Closed tian-yan closed 2 years ago

tian-yan commented 2 years ago

I have an existing post with existing comments already. When I revise the post and save again, I found that existing comments were treated as new comments due to the self.pk in below source code,

django_comments_xtd / models.py

class XtdComment(comment):
....
    def save(self, *args, **kwargs):
            is_new = self.pk is None
            super(Comment, self).save(*args, **kwargs)
            if is_new:

PDB Debug Output for existing comments when revising existing post

[41] > /usr/local/lib/python3.7/site-packages/django_comments_xtd/models.py(73)save()
-> is_new = self.pk is None
(Pdb++) ll
  72         def save(self, *args, **kwargs):                                                                                                                               
  73  ->         is_new = self.pk is None                                                                                                                                   
  74             super(Comment, self).save(*args, **kwargs)                                                                                                                 
  75             if is_new:                                                                                                                                                 
  76                 if not self.parent_id:                                                                                                                                 
  77                     self.parent_id = self.id                                                                                                                           
  78                     self.thread_id = self.id                                                                                                                           

(Pdb++) pp self
<PostComment: django admin: testing comments...>
(Pdb++) pp self.__dict__
{'_state': <django.db.models.base.ModelState object at 0x7fdcebdaced0>,
 '_userinfo': {'email': 'djangoadmin@abc.com', 'name': 'django admin', 'url': ''},
 'comment': 'testing comments...',
 'comment_ptr_id': None,
 'content_type_id': 50,
 'followup': False,
 'id': 17,
 'ip_address': '192.168.100.10',
 'is_public': True,
 'is_removed': False,
 'level': 0,
 'nested_count': 1,
 'object_pk': '19',
 'order': 1,
 'page_id': 19,
 'parent_id': 17,
 'site_id': 1,
 'submit_date': datetime.datetime(2021, 11, 5, 10, 19, 23, 266000, tzinfo=<DstTzInfo 'Asia/Singapore' CST+8:00:00 STD>),
 'thread_id': 17,
 'user_email': 'djangoadmin@abc.com',
 'user_id': 1,
 'user_name': 'djangoadmin',
 'user_url': '',
 'xtdcomment_ptr_id': None}
(Pdb++) pp self.pk
None
(Pdb++) n
[41] > /usr/local/lib/python3.7/site-packages/django_comments_xtd/models.py(74)save()
-> super(Comment, self).save(*args, **kwargs)
(Pdb++) pp is_new
True
(Pdb++) self.id
17
(Pdb++) 

PDB debug output when saving a new comment for existing post.

[33] > /usr/local/lib/python3.7/site-packages/django_comments_xtd/models.py(74)save()
-> super(Comment, self).save(*args, **kwargs)
(Pdb++) pp self.pk
None
(Pdb++) pp self.id
None
(Pdb++) n
[33] > /usr/local/lib/python3.7/site-packages/django_comments_xtd/models.py(75)save()
-> if is_new:
(Pdb++) pp self.pk
19
(Pdb++) pp self.id
19
(Pdb++) pp is_new
True
(Pdb++)

In summary, it seems that every time when I revise the existing posts and save again, the pk of those existing comments become None which causes those existing comments to be treated as NEW, but id still retain its original value.

Q: Shall we use self.id instead of self.pk in above source code ?

danirus commented 2 years ago

Just to be clear, your "post" is a model you have created, like would be an Article or a Story, and then you have comments to that "post". Editing those objects doesn't trigger anything in django-comments-xtd. If it does you must have custom code doing that.

I would suggest you to clone this repository and setup the demo/comp project to have another Django project to compare with. Setting up the demo projects is explained in the documentation.

tian-yan commented 2 years ago

thanks for the reply. I did customize the django-comments-xtd package a bit in my deployment. I will inspect my customized code to find out.