Kazzz Studio
Kazzz Studio

Reputation: 449

Django database model

I am confusing about the postgresql database table made by django. I got information about my user table from psql command. The first colmun, it is last_login field. But I don't set last_login field anywhere.

table definition is the following.

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

And my user model is following. I don't use last_login field. I don't make sense what's going on. Please give me an advice.

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'

Upvotes: 0

Views: 64

Answers (2)

Sam Bobel
Sam Bobel

Reputation: 1824

I wouldn't worry about it. Django does a lot of things included for your future convenience, and fields like that are one of them.

There are a lot of places I could imagine that coming in handy -- making sure to show new stories on a feed, customer service from the admin page, etc.

Don't feel any pressure to use it anywhere, but it doesn't hurt to just leave it be.

Upvotes: 1

varnothing
varnothing

Reputation: 1299

because you have inherited AbstractBaseUser which has last_login field.

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

ref: github source

Upvotes: 3

Related Questions