创建自己的model.py文件
from django.db import models from 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_name 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 Meta: verbose_name="博客信息" verbose_name_plural=verbose_name class 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_name class 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_name 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, 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_name class 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_name class 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_name class 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_name class 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到admin后台
from django.contrib import admin from 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.py AUTH_USER_MODEL = 'jax.UserInfo'