from django.conf import settings from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.models import ContentType from django.db import models class Comment(models.Model): content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) object_id = models.CharField(max_length=255, db_index=True) content_object = GenericForeignKey("content_type", "object_id") author = models.ForeignKey( settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.SET_NULL, related_name="comments" ) parent = models.ForeignKey( "self", null=True, blank=True, on_delete=models.CASCADE, related_name="replies" ) mentions = models.ManyToManyField( settings.AUTH_USER_MODEL, blank=True, related_name="mentioned_comments" ) attachments = models.ManyToManyField( "attachments.Attachment", blank=True, related_name="comments" ) body = models.TextField(blank=True, default="") created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Meta: db_table = "comments" ordering = ["created_at"] indexes = [ models.Index(fields=["content_type", "object_id", "-created_at", "-id"], name="comment_obj_time_idx"), ] def __str__(self): return f"Comment by {self.author or 'deleted user'} on {self.content_type}"