Djiango- model abstract base class

Create an abstract base class model

 

 then

’base_model.py

from django.db Import Models
 from datetime Import DATE 

class : BaseModel (models.Model)
     '' ' model abstract base class ' '' 
    the create_time = models.DateField (auto_now_add = True, the verbose_name = ' Created ' )   # When creating auto_now_add = automatically add the current time 
    update_time = models.DateField (auto_now = True, verbose_name = ' modified ' )   # auto_now = modification is automatically add the current time 
    is_delete = models.BooleanField (default = False, verbose_name = ' marked for deletion ' ) 

    class Meta:
        # Description model is an abstract class 
        abstract = True   # no sentence will complain

 And introduced in succession in other models.py

As 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):
     '' ' Address model class ' '' 
    User = models.ForeignKey ( " the User " , the verbose_name = ' your account ' ) 
    Receiver = models.CharField (= 20 is MAX_LENGTH, the verbose_name = ' recipient ' ) 
    addr = Models. CharField (max_length = 256, verbose_name = ' addressee ' ) 
    ZIP_CODE = models.CharField (max_length = 6, null = True, verbose_name = ' ZIP Code ' ) 
    phone = models.CharField (= max_length 11, verbose_name = ' phone ')
    is_default = models.BooleanField(default=False, verbose_name='是否默认')

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

 

Guess you like

Origin www.cnblogs.com/yifengs/p/11574174.html