from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone

from article.models import ArticlePost
from ckeditor.fields import RichTextField


class Comment(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='comment')
    article = models.ForeignKey(ArticlePost, on_delete=models.CASCADE, related_name='comment')
    body = RichTextField()
    created = models.DateTimeField(default=timezone.now)  # 使用timezone.now()时 进行数据迁移会，有警告
    updated = models.DateTimeField(default=timezone.now)
    total_views = models.PositiveIntegerField(default=0)

    class Meta:
        ordering = ('-created',)

    def __str__(self):
        return self.body[:20]
