博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
django admin后台的简单使用
阅读量:5926 次
发布时间:2019-06-19

本文共 8044 字,大约阅读时间需要 26 分钟。

创建自己的model.py文件

from django.db import modelsfrom django.contrib.auth.models import (    BaseUserManager, AbstractBaseUser, AbstractUser)# class MyUserManager(BaseUserManager):#     def create_user(self, email, name, password=None):#         """#         Creates and saves a User with the given email, date of#         birth and password.#         """#         if not email:#             raise ValueError('Users must have an email address')##         user = self.model(#             email=self.normalize_email(email),#             name=name,#         )##         user.set_password(password)#         user.save(using=self._db)#         return user##     def create_superuser(self, email, name, password):#         """#         Creates and saves a superuser with the given email, date of#         birth and password.#         """#         user = self.create_user(#             email,#             password=password,#             name=name,#         )#         user.is_admin = True#         user.save(using=self._db)#         return user### class MyUser(AbstractBaseUser):#     email = models.EmailField(#         verbose_name='email address',#         max_length=255,#         unique=True,#     )#     # date_of_birth = models.DateField()#     name = models.CharField(max_length=32)#     is_active = models.BooleanField(default=True)#     is_admin = models.BooleanField(default=False)##     objects = MyUserManager()##     USERNAME_FIELD = 'email'#     REQUIRED_FIELDS = ['name']##     def __str__(self):#         return self.email##     def has_perm(self, perm, obj=None):#         "Does the user have a specific permission?"#         # Simplest possible answer: Yes, always#         return True##     def has_module_perms(self, app_label):#         "Does the user have permissions to view the app `app_label`?"#         # Simplest possible answer: Yes, always#         return True##     @property#     def is_staff(self):#         "Is the user a member of staff?"#         # Simplest possible answer: All admins are staff#         return self.is_admin# class UserInfo(AbstractBaseUser):##     avatar = models.ImageField(upload_to="img")## class Blog(models.Model):#     info = models.CharField()## class Artical(models.Model):#     title = models.CharField(max_length=128)#     publish_date = models.DateTimeField()#     author = models.ForeignKey(UserInfo, on_delete=models.CASCADE)#     content = models.OneToOneField(Blog, on_delete=models.CASCADE)## class Commments(models.Model):#     user = models.ForeignKey(UserInfo, on_delete=models.CASCADE)#     c_time = models.DateTimeField()#     comments = models.CharField()#     artical = models.ForeignKey(Artical, on_delete=models.CASCADE)#     reply = models.ForeignKey("Commments", on_delete=models.CASCADE)## class Tag(models.Model):#     tag_name = models.CharField()#     artical = models.ManyToManyField(Artical)## class Category(models.Model):#     name = models.CharField(max_length=64)#     artical = models.ManyToManyField(Artical)## class Praise(models.Model):#     tag = models.IntegerField()#     artical = models.ForeignKey(Artical, on_delete=models.CASCADE)#     user = models.ForeignKey(UserInfo, on_delete=models.CASCADE)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, on_delete=models.CASCADE)    def __str__(self):        return self.username    # class Meta:    #     verbose_name="用户"    #     verbose_name_plural=verbose_nameclass 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 Meta:        verbose_name="博客信息"        verbose_name_plural=verbose_nameclass Category(models.Model):    """    个人博客文章分类    """    nid = models.AutoField(primary_key=True)    title = models.CharField(max_length=32)  # 分类标题    blog = models.ForeignKey(to="Blog", to_field="nid", on_delete=models.CASCADE)  # 外键关联博客,一个博客站点可以有多个分类    def __str__(self):        return self.title    class Meta:        verbose_name="分类"        verbose_name_plural=verbose_nameclass Tag(models.Model):    """    标签    """    nid = models.AutoField(primary_key=True)    title = models.CharField(max_length=32)  # 标签名    blog = models.ForeignKey(to="Blog", to_field="nid", on_delete=models.CASCADE)  # 所属博客    def __str__(self):        return self.title    class Meta:        verbose_name="标签"        verbose_name_plural=verbose_nameclass 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, on_delete=models.CASCADE)    user = models.ForeignKey(to="UserInfo", to_field="nid", on_delete=models.CASCADE)    tags = models.ManyToManyField(  # 中介模型        to="Tag",        through="Article2Tag",        through_fields=("article", "tag"),  # 注意顺序!!!    )    comment_count = models.IntegerField(verbose_name="评论数", default=0)    updown = models.IntegerField(verbose_name="点赞数", default=0)    def __str__(self):        return self.title    class Meta:        verbose_name="文章"        verbose_name_plural=verbose_nameclass ArticleDetail(models.Model):    """    文章详情表    """    nid = models.AutoField(primary_key=True)    content = models.TextField()    article = models.OneToOneField(to="Article", to_field="nid", on_delete=models.CASCADE)    class Meta:        verbose_name="文章内容"        verbose_name_plural=verbose_nameclass Article2Tag(models.Model):    """    文章和标签的多对多关系表    """    nid = models.AutoField(primary_key=True)    article = models.ForeignKey(to="Article", to_field="nid", on_delete=models.CASCADE)    tag = models.ForeignKey(to="Tag", to_field="nid", on_delete=models.CASCADE)    class Meta:        unique_together = (("article", "tag"),)        verbose_name="文章对标签"        verbose_name_plural=verbose_nameclass ArticleUpDown(models.Model):    """    点赞表    """    nid = models.AutoField(primary_key=True)    user = models.ForeignKey(to="UserInfo", null=True, on_delete=models.CASCADE)    article = models.ForeignKey(to="Article", null=True, on_delete=models.CASCADE)    is_up = models.BooleanField(default=True)    class Meta:        unique_together = (("article", "user"),)        verbose_name="点赞"        verbose_name_plural=verbose_nameclass Comment(models.Model):    """    评论表    """    nid = models.AutoField(primary_key=True)    article = models.ForeignKey(to="Article", to_field="nid", on_delete=models.CASCADE)    user = models.ForeignKey(to="UserInfo", to_field="nid", on_delete=models.CASCADE)    content = models.CharField(max_length=255)  # 评论内容    create_time = models.DateTimeField(auto_now_add=True)    parent_comment = models.ForeignKey("self", null=True, blank=True, on_delete=models.CASCADE)    def __str__(self):        return self.content    class Meta:        verbose_name="评论"        verbose_name_plural=verbose_name
models.py

注册自己的models.py到admin后台

from django.contrib import adminfrom jax import models# Register your models here.admin.site.register(models.UserInfo)admin.site.register(models.Blog)admin.site.register(models.Category)admin.site.register(models.Tag)admin.site.register(models.Article)admin.site.register(models.Article2Tag)admin.site.register(models.ArticleDetail)admin.site.register(models.ArticleUpDown)admin.site.register(models.Comment)

扩展django默认的用户表

# settings.py文件还可以更改语言类型

LANGUAGE_CODE = 'zh-hans'
settings.pyAUTH_USER_MODEL = 'jax.UserInfo'

 

转载于:https://www.cnblogs.com/ttyypjt/p/10593969.html

你可能感兴趣的文章
CSharp设计模式读书笔记(10):装饰模式(学习难度:★★★☆☆,使用频率:★★★☆☆)...
查看>>
#、%和$符号在OGNL表达式中的作用
查看>>
java for循环的几种写法
查看>>
个人博客开发-开篇
查看>>
MetroGridHelper: A helpful debugging assistant for designers and developers alike
查看>>
大白话5分钟带你走进人工智能-第二十节逻辑回归和Softmax多分类问题(5)
查看>>
PHP文件 字符集编码!
查看>>
PS 技巧
查看>>
Centos Missing Library: QtWebKit.so.4
查看>>
VC++ 6.0 C8051F340 USB PC侧通信 Demo
查看>>
mysql密码正确却提示错误, 不输入密码反而能登录
查看>>
各变量字节数
查看>>
activemq安全设置 设置admin的用户名和密码
查看>>
卡包模板
查看>>
自适应布局
查看>>
读取数量不定的输入数据
查看>>
选择器
查看>>
GoLang学习笔记——data_type
查看>>
清净布气门功夫介绍
查看>>
c++的进制转换函数
查看>>