1,直接通过 models.ManyToManyField
from django.db import models class UserInfo(models.Model): username = models.CharField(max_length=32) password = models.CharField(max_length=64) class NewsType(models.Model): caption = models.CharField(max_length=16) class News(models.Model): title = models.CharField(verbose_name=‘标题‘,max_length=32) url = models.CharField(verbose_name=‘URL‘,max_length=255) avatar = models.CharField(verbose_name=‘头像‘,max_length=255) summary = models.CharField(verbose_name=‘简介‘,max_length=255) new_type = models.ForeignKey(verbose_name=‘新闻类型‘,to="NewsType") user = models.ForeignKey(verbose_name=‘发布者‘,to=‘UserInfo‘,related_name=‘c‘) ctime = models.DateTimeField(verbose_name=‘创建时间‘,auto_now_add=True) like_count = models.IntegerField(default=0) comment_count = models.IntegerField(default=0) like = models.ManyToManyField(to=‘UserInfo‘) class Comment(models.Model): content = models.CharField(verbose_name=‘评论内容‘,max_length=255) new = models.ForeignKey(verbose_name=‘评论的新闻ID‘,to=‘News‘) user = models.ForeignKey(verbose_name=‘评论者‘,to=‘UserInfo‘) ctime = models.DateTimeField(verbose_name=‘评论时间‘, auto_now_add=True)
时间: 2024-11-02 12:02:10