from django.db import models from django.utils import timezone from article.models import Article from django.contrib.auth.models import User class Comment(models.Model): # 评论人 author = models.ForeignKey( User, on_delete=models.CASCADE, related_name='comments', ) # 文章 article = models.ForeignKey( Article, on_delete=models.CASCADE, related_name='comments', ) # 评论内容 content = models.TextField() # 评论时间 created = models.DateTimeField(default=timezone.now) class Meta: ordering = ['-created'] def __str__(self): # 展示前20个字符 return self.content[:20]