• Djiango-建立模型抽象基类


    创建一个抽象模型基类

     然后

    ’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
  • 相关阅读:
    NKOI 1363 (基础二分)
    POJ 1321 棋盘问题(dfs八皇后变形)
    UVA 1368 DNA(模拟+贪心)
    暴力(解数阵)
    A Linear Algebra Problem(唯一性的判定)
    hdu 1565 方格取数(1)
    hdu 1074 Doing Homework
    hdu 1233 还是畅通工程
    hdu 1232 畅通工程
    hdu 1213 How Many Tables
  • 原文地址:https://www.cnblogs.com/yifengs/p/11574174.html
Copyright © 2020-2023  润新知