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): parent = models.ForeignKey( 'self', null=True, blank=True, on_delete=models.SET_NULL, related_name='children', ) # 评论人 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]