BZ易风

导航

 

创建一个抽象模型基类

 

 然后

’base_model.py

from django.db import models
from datetime import date

class BaseModel(models.Model):
    '''模型抽象基类'''
    create_time = models.DateField(auto_now_add=True, verbose_name='创建时间')  # auto_now_add= 创建时自动添加当前时间
    update_time = models.DateField(auto_now=True, verbose_name='修改时间')  # auto_now= 修改是自动添加当前时间
    is_delete = models.BooleanField(default=False, verbose_name='删除标记')

    class Meta:
        # 说明是一个抽象模型类
        abstract=True  # 没有这句会报错

 在其它的models.py中导入和继承

如 user.models.py

from django.db import models
from django.contrib.auth.models import AbstractUser
from db.base_model import BaseModel
# Create your models here.


class User(AbstractUser, BaseModel):  # 继承模型抽象基类 
    '''用户模型类'''

    class Meta:
        db_table = 'df_user'
        verbose_name = '用户'
        verbose_name_plural = verbose_name


class Address(BaseModel):
    '''地址模型类'''
    user = models.ForeignKey('User', verbose_name='所属账户')
    receiver = models.CharField(max_length=20, verbose_name='收件人')
    addr = models.CharField(max_length=256, verbose_name='收件地址')
    zip_code = models.CharField(max_length=6, null=True, verbose_name='邮政编码')
    phone = models.CharField(max_length=11, verbose_name='联系电话')
    is_default = models.BooleanField(default=False, verbose_name='是否默认')

    class Meta:
        db_table = 'df_address'
        verbose_name = '地址'
        verbose_name_plural = verbose_name

 

posted on 2019-09-23 19:17  BZ易风  阅读(389)  评论(0编辑  收藏  举报