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
594 stars 158 forks source link

Document use case: Delete All Related Comments When Object Deleted #197

Open jimkring opened 4 years ago

jimkring commented 4 years ago

It would be great to document the use case of how to delete all comments associated with an object, when the object is deleted. I asked this question and got a great answer by @danirus here: https://github.com/danirus/django-comments-xtd/issues/193#issuecomment-672658623

I tested the following code, inside of my models.py for myapp and it works great!

from django.db import models
from django.db.models.signals import post_delete
from django.dispatch import receiver

from django_comments_xtd.models import ContentType, XtdComment

from myapp.models import MyModel

@receiver(post_delete, sender=MyModel)
def delete_comments(sender, instance, **kwargs):
    ct = ContentType.objects.get(app_label="myapp", model="mymodel")
    XtdComment.objects.filter(content_type=ct, object_pk=instance.id).delete()
danirus commented 4 years ago

Indeed, why not. Would you like to document it?

jimkring commented 4 years ago

Yes! I’d be happy to learn how to contribute to the use case docs. I’ll see if I can figure it out, and please let me know if you have any directions you’d like to give me.

danirus commented 4 years ago

Sounds good!

The docs are built with Sphinx. I also install sphinx-rtd-theme and sphinx-autobuild.

With those packages I cd in the docs/ directory and run sphinx-autobuild -p 1025 . _build/ to have sphinx watching the files and reloading the browser with the changes. That command runs a small http server in port 1025.

Regarding the use cases in the docs, for every use case, I create a file in the docs/usecases/ directory and add an entry to point to it in the docs/usecases.rst file.

To write the new use case, fork the repository and work on the changes in your copy of the repository. Whenever you are ready to get a review of them simply file a Pull Request against this repository and I will see them right away.