码迷,mamicode.com
首页 > 数据库 > 详细

数据库 表关系设计参考

时间:2018-05-10 23:43:32      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:iss   外键   lis   __str__   详细   avatar   数据库   city   Nid   

表关系设计

出版社表关系

# 出版社表
class Publisher(models.Model):
    name = models.CharField(max_length=32)
    addr = models.CharField(max_length=128)
    phone = models.IntegerField()

    def __str__(self):
        return self.name


# 作者表
class Author(models.Model):
    name = models.CharField(max_length=16)
    # 作者和详情表示一对一关联的
    detail = models.OneToOneField(to="AuthorDetail")
    # 作者和书的关系是多对多
    books = models.ManyToManyField(to="Book")

    def __str__(self):
        return self.name

# 作者详情表
class AuthorDetail(models.Model):
    city = models.CharField(max_length=32)
    email = models.EmailField()


class Book(models.Model):
    title = models.CharField(max_length=16)
    price = models.DecimalField(max_digits=5, decimal_places=2)  # 5位数字,两位小数 999.99
    # auto_now=True 每一次修改都自动更新时间;auto_now_add=True 第一次创建时自动填写时间
    publish_day = models.DateField(auto_now_add=True)
    # 书和出版社是多对一,通过外键关联
    publisher = models.ForeignKey(to="Publisher")

    memo = models.CharField(max_length=128, null=True)

    def __str__(self):
        return self.title

博客系统表关系

from django.contrib.auth.models import AbstractUser


class UserInfo(AbstractUser):
    """
    用户信息
    """
    nid = models.AutoField(primary_key=True)
    nickname = models.CharField(verbose_name='昵称', max_length=32)
    telephone = models.CharField(max_length=11, null=True, unique=True)
    avatar = models.FileField(upload_to = 'avatars/',default="/avatars/default.png")
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    blog = models.OneToOneField(to='Blog', to_field='nid',null=True)

    def __str__(self):
        return self.username

class Blog(models.Model):

    """
    博客信息
    """
    nid = models.AutoField(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)

    def __str__(self):
        return self.title

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 self.title
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 self.title

class Article(models.Model):

    nid = models.AutoField(primary_key=True)
    title = models.CharField(max_length=50, verbose_name='文章标题')
    desc = models.CharField(max_length=255, verbose_name='文章描述')

    comment_count= models.IntegerField(default=0)
    up_count = models.IntegerField(default=0)
    down_count = models.IntegerField(default=0)

    create_time = models.DateTimeField(verbose_name='创建时间')

    homeCategory = models.ForeignKey(to='Category', to_field='nid', null=True)
    #siteDetaiCategory = models.ForeignKey(to='SiteCategory', to_field='nid', null=True)

    user = models.ForeignKey(verbose_name='作者', to='UserInfo', to_field='nid')
    tags = models.ManyToManyField(
        to="Tag",
        through='Article2Tag',
        through_fields=('article', 'tag'),
    )


    def __str__(self):
        return self.title

class ArticleDetail(models.Model):
    """
    文章详细表
    """
    nid = models.AutoField(primary_key=True)
    content = models.TextField()
    article = models.OneToOneField(to='Article', to_field='nid')

class Comment(models.Model):
    """
    评论表
    """
    nid = models.AutoField(primary_key=True)
    article = models.ForeignKey(verbose_name='评论文章', to='Article', to_field='nid')
    user = models.ForeignKey(verbose_name='评论者', to='UserInfo', to_field='nid')
    content = models.CharField(verbose_name='评论内容', max_length=255)
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)

    parent_comment = models.ForeignKey('self', null=True)


    def __str__(self):
        return self.content

class ArticleUpDown(models.Model):
    """
    点赞表
    """
    nid = models.AutoField(primary_key=True)
    user = models.ForeignKey('UserInfo', null=True)
    article = models.ForeignKey("Article", null=True)
    is_up=models.BooleanField(default=True)

    class Meta:
        unique_together = [
            ('article', 'user'),
        ]

class Article2Tag(models.Model):
    nid = models.AutoField(primary_key=True)
    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'),
        ]

    def __str__(self):
        v=self.article.title+"----"+self.tag.title
        return v

权限系统表关系

class Menu(models.Model):
    caption=models.CharField(max_length=32)

    def __str__(self):
        return self.caption

class UserInfo(models.Model):
    name=models.CharField(max_length=32)
    pwd=models.CharField(max_length=32,default=123)
    email=models.EmailField()
    roles=models.ManyToManyField(to="Role")

    def __str__(self):
        return self.name

class Role(models.Model):
    title=models.CharField(max_length=32)
    permissions=models.ManyToManyField(to="Permission")
    def __str__(self):
        return self.title

class Permission(models.Model):
    url=models.CharField(max_length=32)
    title=models.CharField(max_length=32)
    permission_group=models.ForeignKey("PermissionGroup",default=1)
    code=models.CharField(max_length=32,default="")
    parent=models.ForeignKey("self",default=1,null=True,blank=True)
    def __str__(self):
        return self.title

class PermissionGroup(models.Model):
    caption=models.CharField(max_length=32)
    menu=models.ForeignKey("Menu",default=1)
    def __str__(self):
        return self.caption

数据库 表关系设计参考

标签:iss   外键   lis   __str__   详细   avatar   数据库   city   Nid   

原文地址:https://www.cnblogs.com/iyouyue/p/9021820.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!