0

So, I have a Comment model in a django project I am trying to make, what I want to do is to override the on_delete function of the ForeignKey

COMMENT_TYPES = (
(1, "Comment"),
(2, "Reply to a comment"))

class Comment(models.Model):
   author = models.ForeignKey(User, on_delete=models.CASCADE)
   comment_text = models.CharField(max_length=80, blank=False, null=False)
   id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True,editable=False)
   date_created = models.DateTimeField(auto_now_add=True)
   date_edited = models.DateTimeField(auto_now=True)
   post = models.ForeignKey(Post, on_delete=models.CASCADE)
   parent_comment = models.ForeignKey('self', blank=True, null=True, on_delete=models.SET_NULL)
   comment_type = models.IntegerField(choices=COMMENT_TYPES, default=1)

What I want is that when I delete a comment, then all the comments that were linked to the deleted comment, their comment_type should change from 2('reply to a comment') to 1('comment'), so is there a way to override the on_delete function, SET_NULL so that I can change the value in the comment_type field?

  • https://docs.djangoproject.com/en/4.1/topics/signals/ – C14L Oct 09 '22 at 15:20
  • In my opinion, the foreign comment should not be deleted! It is better to set it only visible for repliers and the author. – Waket Zheng Oct 09 '22 at 15:25
  • @WaketZheng See, what I mean is when some user (let's say USER_X) deletes his comment, then I want to change the comment_type of all the replies to USER_X's deleted comment to comment and not reply to comment, so what you are trying to say doesn't exactly solve my problem – Aryan Shandilya Oct 09 '22 at 15:30
  • @C14L So, what you are trying to point out is that, I setup a pre_delete or a post_delete signal that will change all value of comment_type for all comment objects that are linked to the to-be-deleted comment, so I do something like `obj.comment_set.all()`, obj is the comment to be deleted and change value for each of the objects from that list? If I do this, for a large number of comments will the process be very slow, if yes, what should I do in that case? – Aryan Shandilya Oct 09 '22 at 15:37
  • If it is slow for this type of cases, imho, change database. you never have more than about 100 ? 1000 comment to update ? Any database can make this operation in less than 1 second... – Lucas Grugru Oct 09 '22 at 15:47
  • `Comment.objects.filter(parent_comment=comment_to_be_deleted).update(comment_type=1)` this did what I wanted, I got help from this https://stackoverflow.com/questions/14035198/django-bulk-operations .Though I don't know if it is the most efficient way even today. – Aryan Shandilya Oct 09 '22 at 16:53
  • Yes. For speed, make sure to have an index set on the `parent_comment` column in your Django model. – C14L Oct 09 '22 at 21:33
  • 1
    Actually, you could drop the `comment_type` column entirely and simply check if a comment's `parent_comment` is `NULL` (top comment) or `NOT NULL` (reply to a comment). – C14L Oct 09 '22 at 21:34
  • @C14L Even I think that would be the best way I will just remove comment_type entirely. – Aryan Shandilya Oct 10 '22 at 03:52

0 Answers0