zoukankan      html  css  js  c++  java
  • day 82 Django Admin组件.

    一、先建表环境

    modules文件

    from django.db import models
    
    # Create your models here.
    from django.contrib.auth.models import AbstractUser
    
    class UserInfo(AbstractUser):
        '''
        用户表
        '''
        nid  =models.AutoField(primary_key=True)
        phone  =models.CharField(max_length=11,null=True,unique=True)
        avatar = models.FileField(upload_to='avatars/',default='avatars/default.png',verbose_name='头像')
        create_time =models.DateTimeField(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(max_length=64)
        site =models.CharField(max_length=32,unique=True)
        theme =models.CharField(max_length=32)
    
        def __str__(self):
            return self.title
    #
    
    class  Category(models.Model):
        '''
        个人博客文章分类
        '''
        nid =models.AutoField(primary_key=True)
        title =models.CharField(max_length=32)#分类标题
        blog = models.ForeignKey(to ='Blog',to_field='nid')#外键关联博客,一个博客站点可以有多个分类
    
        def __str__(self):
            return self.title
    
    class Tag(models.Model):
        ''''
        标签
        '''
        nid =models.AutoField(primary_key=True)
        title =models.CharField(max_length=32)
        blog =models.ForeignKey(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) #文章标题描述
        desc =models.CharField(max_length=255) #文章描述
        create_time =models.DateTimeField()#创建时间
    
        category = models.ForeignKey(to ='Category',to_field='nid',null=True)
        user = models.ForeignKey(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 Article2Tag(models.Model):
        '''
        文章和标签的多对多关系表
        '''
        nid =models.AutoField(primary_key=True)
        article =models.ForeignKey(to='Article',to_field='nid')
        tag =models.ForeignKey(to ='Tag',to_field='nid')
    
        class Meta:
            unique_together = (('article','tag'),)
    
    class ArticleUpDown(models.Model):
        '''
        点赞
        '''
        nid= models.AutoField(primary_key=True)
        user =models.ForeignKey(to ='UserInfo',null=True)
        article = models.BooleanField(default=True)
    
        class  Meta:
            unique_together = (('article','user'),)
    
    class Comment(models.Model):
        '''
        评论表
        '''
        nid = models.AutoField(primary_key=True)
        article =models.ForeignKey(to ='Article',to_field='nid')
        user =models.ForeignKey(to ='UserInfo',to_field='nid')
        content =models.CharField(max_length=255)#评论内容
        create_time =models.DateTimeField(auto_now_add=True)
        parent_comment =models.ForeignKey('self',null=True)
    
        def __str__(self):
            return self.content
    

     

    # 告诉Django项目用哪张表做认证
    
    AUTH_USER_MODEL ='app01.UserInfo'
    

    新建一个管理员账号 

    admin / dadmin01 

     通过web去登录 

    修改显示的内容

     

    展示的内容

    设置某个字段为连接字段

     增加方法字段:

     

    批量初始化 

    重写页面

     

     

     

    Admin的实现流程

    1. 启动

       import admin 

      def autodiscover():

        autodiscover_modules('admin', register_to=site)

      执行每个app下的admin.py文件

      

     

    2. 注册 (单例模式 singlelton pattern)

    3. 

     

     

    单例模式 

     

    # class Person(object):
    #
    #     def __init__(self,name,age):
    #         self.name=name
    #         self.age=age
    #
    #
    # alex=Person("alex",33)
    # egon=Person("egon",32)
    

      

    # 单例模式方式1 :__new__

    # class Singleton(object):
    #
    #     _instance = None
    #     def __new__(cls, *args, **kw):
    #         if not cls._instance:
    #             cls._instance = super(Singleton, cls).__new__(cls, *args, **kw)
    #         return cls._instance
    #
    # class MyClass(Singleton):
    #     a = 1
    #
    #
    #
    # mc1=MyClass()
    #
    # mc2=MyClass()
    #
    # mc3=MyClass()
    
    
    # print(id(mc1))
    # print(id(mc2))
    # print(id(mc3))
    

      

    # 单例模式方式2 :模块方式

     

    # 思考1
    # from mysingleton import my_singleton as my_singleton_new
    #
    # print(id(my_singleton_new)) 
    # print(id(my_singleton))
    
    # 思考2
    # import func
    #
    # func.bar()
    
    # 思考3
    
    from mysingleton import my_singleton,My_Singleton
    
    ms1=My_Singleton()
    
    from mysingleton import my_singleton,My_Singleton
    
    ms2=My_Singleton()
    
    print(id(ms1))
    print(id(ms2))
    

      

  • 相关阅读:
    [leetcode]95 Unique Binary Search Trees II (Medium)
    [leetcode] 96 Unique Binary Search Trees (Medium)
    [leetcode] 72. Edit Distance (hard)
    [leetcode] 120. Triangle (Medium)
    [leetcode] 63. Unique Paths II (medium)
    [OpenGL] 不规则区域的填充算法
    [leetcode] 64. Minimum Path Sum (medium)
    ESLint入坑
    报错:for..in loops iterate over the entire prototype chain, which is virtually never what you want.
    vue解决seo优化之预渲染prerender-spa-plugin
  • 原文地址:https://www.cnblogs.com/mengbin0546/p/9117528.html
Copyright © 2011-2022 走看看