2017-09-06 161 views
0

我對django製作的postgresql數據庫表感到困惑。我從psql命令獲得了有關我的用戶表的信息。第一個colmun,它是last_login字段。但是我不會在任何地方設置last_login字段。Django數據庫模型

表定義如下。

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' 

回答

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

+0

謝謝你的回覆。這對我很有幫助。 –

1

我就不會擔心。 Django爲了您將來的便利做了很多事情,像這樣的領域就是其中之一。

有很多的地方,我可以想像,在未來很方便 - 確保上顯示從管理頁面飼料,服務客戶的新故事等

沒有感到任何壓力,使用它在任何地方,但它不會傷害只是離開它。