Django数据库模型

时间:2017-09-06 17:15:10

标签: django python-3.x django-models

我对django制作的postgresql数据库表感到困惑。我从psql命令获得了有关我的用户表的信息。第一个colmun,它是last_login字段。但是我没有在任何地方设置last_login字段。

表定义如下。

remoshin_devdb=# \d remosys_remoshin_user_tbl;
    Table "public.remosys_remoshin_user_tbl"
    Column      |           Type           | Modifiers
-----------------+--------------------------+-----------
 last_login      | timestamp with time zone |
 userid          | character varying(64)    | not null
 username        | character varying(128)   | not null
 email           | character varying(254)   | not null
 password        | character varying(128)   | not null
 usertype        | character varying(1)     | not null
 is_active       | character varying(1)     | not null
 is_admin        | character varying(1)     | not null
 u_kana_name     | character varying(128)   |
 u_date_of_birth | date                     |
 u_gender        | character varying(1)     | not null
 u_postno        | character varying(7)     |
 u_address1      | character varying(128)   |
 u_address2      | character varying(128)   |
 u_telno         | character varying(16)    |
 u_photo         | character varying(100)   |
 d_photo         | character varying(100)   |
 create_date     | timestamp with time zone | not null
 modify_date     | timestamp with time zone | not null
 d_clinic_id_id  | character varying(8)     |
Indexes:
    "remosys_remoshin_user_tbl_pkey" PRIMARY KEY, btree (userid)
    "remosys_remoshin_user_tbl_email_key" UNIQUE CONSTRAINT, btree (email)
    "remosys_remoshin_user_tbl_d_clinic_id_id_65dbde14" btree (d_clinic_id_id)
    "remosys_remoshin_user_tbl_d_clinic_id_id_65dbde14_like" btree (d_clinic_id_id varchar_pattern_ops)
    "remosys_remoshin_user_tbl_email_4dc9031b_like" btree (email varchar_pattern_ops)
    "remosys_remoshin_user_tbl_userid_e92979ce_like" btree (userid varchar_pattern_ops)
Foreign-key constraints:
    "remosys_remoshin_use_d_clinic_id_id_65dbde14_fk_remosys_c" FOREIGN KEY (d_clinic_id_id) REFERENCES remosys_clinic_tbl(clinic_id) DEFERRABLE 
INITIALLY DEFERRED
Referenced by:
    TABLE "authtoken_token" CONSTRAINT "authtoken_token_user_id_35299eff_fk_remosys_r" FOREIGN KEY (user_id) REFERENCES remosys_remoshin_user_tbl(userid) DEFERRABLE INITIALLY DEFERRED
    TABLE "django_admin_log" CONSTRAINT "django_admin_log_user_id_c564eba6_fk_remosys_r" FOREIGN KEY (user_id) REFERENCES remosys_remoshin_user_tbl(userid) DEFERRABLE INITIALLY DEFERRED
    TABLE "remosys_consultation_menu_tbl" CONSTRAINT "remosys_consultation_user_email_id_4e50d1dc_fk_remosys_r" FOREIGN KEY (user_email_id) REFERENCES remosys_remoshin_user_tbl(userid) DEFERRABLE 
    INITIALLY DEFERRED
    TABLE "remosys_consultation_tbl" CONSTRAINT "remosys_consultation_user_id_5541f93d_fk_remosys_r" FOREIGN KEY (user_id) REFERENCES remosys_remoshin_user_tbl(userid) DEFERRABLE INITIALLY DEFERRED
TABLE "remosys_refund_request_tbl" CONSTRAINT "remosys_refund_reque_user_email_id_f46e5866_fk_remosys_r" FOREIGN KEY (user_email_id) REFERENCES remosys_remoshin_user_tbl(userid) DEFERRABLE 
INITIALLY DEFERRED

我的用户模型如下。我没有使用last_login字段。我不知道发生了什么。请给我一个建议。

class RemoshinUserManager(BaseUserManager):

    def create_user(self, userid, username, email, password):
        """ Creates and saves User with the given email and password. """
        # now = timezone.now()
        if not email:
            raise ValueError('Users must have an email address.')
        email = self.normalize_email(email),
        user = self.model(
            userid=userid,
            username=username,
            email=email,
            is_active='A',
            # last_login=now,
            # date_joined=now,
            # **extra_fields
        )

        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, userid, username, email, password):
        """ Creates and saves a superuser with the given email and password. """
        user = self.create_user(userid, username, email, password)
        user.is_active = 'A'
        # user.is_staff = True
        user.is_admin = 'A'
        # user.is_superuser = True
        user.save(using=self._db)
        return user


class RemoshinUser(AbstractBaseUser):
    userid = models.CharField('userID', max_length=64, primary_key=True)
    username = models.CharField('user name', max_length=128)
    email = models.EmailField('email address', unique=True)
    password = models.CharField('password', max_length=128)

    USERTYPE_CHOICE = (
        ('U', 'end user'),
        ('C', 'doctor'),
        ('A', 'administrator'),
    )
    usertype = models.CharField('user type', max_length=1, choices=USERTYPE_CHOICE, default='U')

    # is_authenticated = models.BooleanField('authentication flag', default=True)

    ACTIVETYPE_CHOICE = {
        ('A', 'active'),
        ('I', 'inactive'),
    }
    is_active = models.CharField('active flag', max_length=1, choices=ACTIVETYPE_CHOICE, default='A')

    ADMINTYPE_CHOICE = {
        ('A', 'administrator'),
        ('N', 'normal user'),
    }
    is_admin = models.CharField('administrator flag', max_length=1, choices=ADMINTYPE_CHOICE, default='N')

    # For Enduser
    u_kana_name = models.CharField('user kana name', max_length=128, blank=True, null=True)
    u_date_of_birth = models.DateField('date of birth', blank=True, null=True)
    # u_gender = models.SmallIntegerField('gender', blank=True, null=True)

    GENDER_CHOICES = (
        ('M', 'male'),
        ('F', 'female'),
    )
    u_gender = models.CharField('gender', max_length=1, choices=GENDER_CHOICES, default='M')

    u_postno = models.CharField('post no', max_length=7, blank=True, null=True)
    u_address1 = models.CharField('address 1', max_length=128, blank=True, null=True)
    u_address2 = models.CharField('address 2', max_length=128, blank=True, null=True)
    u_telno = models.CharField('tel no', max_length=16, blank=True, null=True)
    u_photo = models.ImageField('user photo', upload_to='users/%Y/%m/%d', blank=True, null=True)

    # For Doctor
    d_clinic_id = models.ForeignKey(Clinic, blank=True, null=True)
    d_photo = models.ImageField('doctor photo', blank=True, null=True)

    # For administrator
    # No column is defined.

    create_date = models.DateTimeField('create date', default=timezone.now)
    modify_date = models.DateTimeField('modify date', default=timezone.now)

    objects = RemoshinUserManager()

    USERNAME_FIELD  = 'userid'
    REQUIRED_FIELDS = ['username', 'email']

    class Meta:
        db_table = 'remosys_remoshin_user_tbl'

2 个答案:

答案 0 :(得分:3)

因为您继承了AbstractBaseUser,其中包含last_login字段。

class AbstractBaseUser(models.Model):
    password = models.CharField(_('password'), max_length=128)
    last_login = models.DateTimeField(_('last login'), blank=True, null=True)
    # .
    # .
    # .

参考:github source

答案 1 :(得分:1)

我不担心。为了您将来的方便,Django会做很多事情,像这样的领域就是其中之一。

我可以想象很多地方派上用场 - 确保在Feed页面上显示新故事,从管理页面显示客户服务等。

在任何地方都不要感到任何压力,但只要离开就不会有任何伤害。