首页 > 代码库 > 报障系统之填充基本数据
报障系统之填充基本数据
通过Django admin填充基本数据:
#admin.pyfrom django.contrib import adminfrom app01 import modelsadmin.site.register(models.Article)admin.site.register(models.Article2Tag)admin.site.register(models.ArticleDetail)admin.site.register(models.Blog)admin.site.register(models.Category)admin.site.register(models.Comment)admin.site.register(models.Tag)admin.site.register(models.UpDown)admin.site.register(models.UserFans)admin.site.register(models.UserInfo)#创建用户:python manage.py createsuperuser#访问:127.0.0.1:8000/admin/#models.pyfrom django.db import modelsclass UserInfo(models.Model): """ 用户表 """ nid = models.BigAutoField(primary_key=True) username = models.CharField(verbose_name=‘用户名‘, max_length=32, unique=True) password = models.CharField(verbose_name=‘密码‘, max_length=64) nickname = models.CharField(verbose_name=‘昵称‘, max_length=32) email = models.EmailField(verbose_name=‘邮箱‘, unique=True) avatar = models.ImageField(verbose_name=‘头像‘,upload_to=‘static/images‘) create_time = models.DateTimeField(verbose_name=‘创建时间‘, auto_now_add=True) fans = models.ManyToManyField(verbose_name=‘粉丝们‘, to=‘UserInfo‘, through=‘UserFans‘, related_name=‘f‘, through_fields=(‘user‘, ‘follower‘)) def __str__(self): return self.nicknameclass Blog(models.Model): """ 博客信息 """ nid = models.BigAutoField(primary_key=True) title = models.CharField(verbose_name=‘个人博客标题‘, max_length=64) site = models.CharField(verbose_name=‘个人博客后缀‘, max_length=32, unique=True) theme = models.CharField(verbose_name=‘博客主题‘, max_length=32) user = models.OneToOneField(to=‘UserInfo‘, to_field=‘nid‘) def __str__(self): return self.titleclass UserFans(models.Model): """ 互粉关系表 """ user = models.ForeignKey(verbose_name=‘博主‘, to=‘UserInfo‘, to_field=‘nid‘, related_name=‘users‘) follower = models.ForeignKey(verbose_name=‘粉丝‘, to=‘UserInfo‘, to_field=‘nid‘, related_name=‘followers‘) class Meta: unique_together = [ (‘user‘, ‘follower‘), ]class Category(models.Model): """ 博主个人文章分类表 """ nid = models.AutoField(primary_key=True) title = models.CharField(verbose_name=‘分类标题‘, max_length=32) blog = models.ForeignKey(verbose_name=‘所属博客‘, to=‘Blog‘, to_field=‘nid‘) def __str__(self): return "%s-%s" %(self.blog.title,self.title)class ArticleDetail(models.Model): """ 文章详细表 """ content = models.TextField(verbose_name=‘文章内容‘, ) article = models.OneToOneField(verbose_name=‘所属文章‘, to=‘Article‘, to_field=‘nid‘)class UpDown(models.Model): """ 文章顶或踩 """ article = models.ForeignKey(verbose_name=‘文章‘, to=‘Article‘, to_field=‘nid‘) user = models.ForeignKey(verbose_name=‘赞或踩用户‘, to=‘UserInfo‘, to_field=‘nid‘) up = models.BooleanField(verbose_name=‘是否赞‘) class Meta: unique_together = [ (‘article‘, ‘user‘), ]class Comment(models.Model): """ 评论表 """ nid = models.BigAutoField(primary_key=True) content = models.CharField(verbose_name=‘评论内容‘, max_length=255) create_time = models.DateTimeField(verbose_name=‘创建时间‘, auto_now_add=True) reply = models.ForeignKey(verbose_name=‘回复评论‘, to=‘self‘, related_name=‘back‘, null=True) article = models.ForeignKey(verbose_name=‘评论文章‘, to=‘Article‘, to_field=‘nid‘) user = models.ForeignKey(verbose_name=‘评论者‘, to=‘UserInfo‘, to_field=‘nid‘)class Tag(models.Model): nid = models.AutoField(primary_key=True) title = models.CharField(verbose_name=‘标签名称‘, max_length=32) blog = models.ForeignKey(verbose_name=‘所属博客‘, to=‘Blog‘, to_field=‘nid‘) def __str__(self): return "%s-%s" %(self.blog.title,self.title)class Article(models.Model): nid = models.BigAutoField(primary_key=True) title = models.CharField(verbose_name=‘文章标题‘, max_length=128) summary = models.CharField(verbose_name=‘文章简介‘, max_length=255) read_count = models.IntegerField(default=0) comment_count = models.IntegerField(default=0) up_count = models.IntegerField(default=0) down_count = models.IntegerField(default=0) create_time = models.DateTimeField(verbose_name=‘创建时间‘, auto_now_add=True) blog = models.ForeignKey(verbose_name=‘所属博客‘, to=‘Blog‘, to_field=‘nid‘) category = models.ForeignKey(verbose_name=‘文章类型‘, to=‘Category‘, to_field=‘nid‘, null=True) type_choices = [ (1, "Python"), (2, "Linux"), (3, "OpenStack"), (4, "GoLang"), ] # 网站分类 article_type_id = models.IntegerField(choices=type_choices, default=None) tags = models.ManyToManyField( to="Tag", through=‘Article2Tag‘, through_fields=(‘article‘, ‘tag‘), ) def __str__(self): return "%s-%s" %(self.blog.title,self.title)class Article2Tag(models.Model): article = models.ForeignKey(verbose_name=‘文章‘, to="Article", to_field=‘nid‘) tag = models.ForeignKey(verbose_name=‘标签‘, to="Tag", to_field=‘nid‘) class Meta: unique_together = [ (‘article‘, ‘tag‘), ]
报障系统之个人博客主页:
报障系统之填充基本数据
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。