models.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from django.db import models
  2. from django.contrib.auth.models import User
  3. from django.utils import timezone
  4. from article.models import ArticlePost
  5. from ckeditor.fields import RichTextField
  6. from mptt.models import MPTTModel, TreeForeignKey
  7. class Comment(MPTTModel):
  8. user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='comment')
  9. article = models.ForeignKey(ArticlePost, on_delete=models.CASCADE, related_name='comment')
  10. body = RichTextField()
  11. created = models.DateTimeField(default=timezone.now) # 使用timezone.now()时 进行数据迁移会,有警告
  12. updated = models.DateTimeField(default=timezone.now)
  13. total_views = models.PositiveIntegerField(default=0)
  14. # mptt树形结构
  15. parent = TreeForeignKey(
  16. 'self',
  17. on_delete=models.CASCADE,
  18. null=True,
  19. blank=True,
  20. related_name='children'
  21. )
  22. # 记录二级评论回复给谁,str
  23. reply_to = models.ForeignKey(
  24. User,
  25. null=True,
  26. blank=True,
  27. on_delete=models.CASCADE,
  28. related_name='replyers'
  29. )
  30. # class Meta:
  31. # ordering = ('-created',)
  32. class MPTTMeta:
  33. order_insertion_by = ['created']
  34. def __str__(self):
  35. return self.body[:20]