使用AbstractBaseUser类时,Django模型迁移添加了两个不需要的字段

时间:2016-09-09 08:33:36

标签: python mysql django django-models

Django 1.9
Python 3.4

我使用AbstractBaseUser类创建了一个自定义用户模型。以下是代码。

class UserModel(AbstractBaseUser):
    # custom user class
    SYSTEM = 0
    TENANT = 1
    parent_type_choices = (
                           (SYSTEM, 'System'),
                           (TENANT, 'Tenant')
                           )
    sys_id = models.BigIntegerField(primary_key=True, blank=False)
    parent_type = models.PositiveIntegerField(choices=parent_type_choices, null=False, blank=False)
    parent_sys_id = models.ForeignKey('tenant.TenantModel', on_delete = models.SET_NULL, null=True, blank=True)
    last_name = models.CharField(null=False, blank=False, max_length=40)
    first_name = models.CharField(max_length=40, null=False, blank=False)
    display_name = models.CharField(max_length=80, unique=True, null=False, blank=False)
    login = models.CharField(max_length=40, unique=True, null=False, blank=False)
    authentication_method = models.CharField(max_length=80)
    pwd = models.CharField(max_length=40)
    access_valid_start = models.DateTimeField()
    access_valid_end = models.DateTimeField()
    created_when = models.DateTimeField()
    created_by = models.BigIntegerField()
    last_updated_when = models.DateTimeField()
    last_updated_by = models.BigIntegerField()
    notes = models.CharField(max_length=2048)

    USERNAME_FIELD = "login"

    class Meta:
        app_label = "accounts"
        db_table = "Users"

当我迁移更改时,表在db中创建了两个额外的字段,我没有定义。 Passwordlast_login已添加。

desc Users;
+-----------------------+------------------+------+-----+---------+-------+
| Field                 | Type             | Null | Key | Default | Extra |
+-----------------------+------------------+------+-----+---------+-------+
| password              | varchar(128)     | NO   |     | NULL    |       |
| last_login            | datetime         | YES  |     | NULL    |       |
| sys_id                | bigint(20)       | NO   | PRI | NULL    |       |
| parent_type           | int(10) unsigned | NO   |     | NULL    |       |
| last_name             | varchar(40)      | NO   |     | NULL    |       |
| first_name            | varchar(40)      | NO   |     | NULL    |       |
| display_name          | varchar(80)      | NO   | UNI | NULL    |       |
| login                 | varchar(40)      | NO   | UNI | NULL    |       |
| authentication_method | varchar(80)      | NO   |     | NULL    |       |
| pwd                   | varchar(40)      | NO   |     | NULL    |       |
| access_valid_start    | datetime         | NO   |     | NULL    |       |
| access_valid_end      | datetime         | NO   |     | NULL    |       |
| created_when          | datetime         | NO   |     | NULL    |       |
| created_by            | bigint(20)       | NO   |     | NULL    |       |
| last_updated_when     | datetime         | NO   |     | NULL    |       |
| last_updated_by       | bigint(20)       | NO   |     | NULL    |       |
| notes                 | varchar(2048)    | NO   |     | NULL    |       |
| parent_sys_id_id      | bigint(20)       | YES  | MUL | NULL    |       |
+-----------------------+------------------+------+-----+---------+-------+

为什么会这样?如何删除这些字段?

1 个答案:

答案 0 :(得分:0)

您继承自提供这些字段的AbstractBaseUser类。你无法删除它们。查看Django doc了解详情