报障系统之填充基本数据

Posted luchuangao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了报障系统之填充基本数据相关的知识,希望对你有一定的参考价值。

 

通过Django admin填充基本数据:

#admin.py
from django.contrib import admin
from app01 import models

admin.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.py

from django.db import models


class 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.nickname
class 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.title


class 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‘),
        ]

 报障系统之个人博客主页:

 

以上是关于报障系统之填充基本数据的主要内容,如果未能解决你的问题,请参考以下文章

报障系统之博客主页及后台管理

报障系统之多级评论

报障系统之多级评论前戏

django报障系统之解决工单和报表

在一些片段之间填充对象变量的最佳方法

python 报障系统(完)