0

我在 Django 中创建了一个自定义用户,并向 UserProfile 模型添加了一些额外的字段。我想保留所有不带下划线的列名,但 last_login 字段是由基本用户创建的。运行迁移后,用户表中唯一带有下划线的列是“last_login”。请告知如何将该字段/列名称更改为“lastlogin”。

models.py 看起来像这样 -

class CustomUserManager(BaseUserManager):

    def _create_user(self, username, email, password,
                     is_staff, is_superuser, **extra_fields):
        """
        Creates and saves a User with the given username, email and password.
        """
        if not username:
            raise ValueError('The given username must be set')
        email = self.normalize_email(email)
        user = self.model(username=username, email=email,
                          is_staff=is_staff, is_active=True,
                          is_superuser=is_superuser, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, username, email=None, password=None,
                    **extra_fields):
        return self._create_user(username, email, password, False, False,
                                 **extra_fields)

    def create_superuser(self, username, email, password, **extra_fields):
        return self._create_user(username, email, password, True, True,
                                 **extra_fields)


class UserProfile(AbstractBaseUser):

    class Meta:
        """
        meta class for user role, defining ordering and db table name
        """
        ordering = ["name"]
        db_table = 'user'

    username = models.CharField(max_length=254, unique=True)
    name = models.CharField(max_length=254, blank=True)
    email = models.EmailField(blank=True, unique=True)
    createdon = models.DateTimeField(auto_now_add=True)
    updatedon = models.DateTimeField(auto_now=True)
    is_active = models.BooleanField(default=True, db_column='status')
    is_admin = models.BooleanField(default=False, db_column='isadmin')
    is_staff = models.BooleanField(default=False, db_column='isstaff')
    is_superuser = models.BooleanField(default=False, db_column='issuperuser')

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

    objects = CustomUserManager()

    def get_full_name(self):
        return self.username

    def get_short_name(self):
        return self.username
4

1 回答 1

0

我在这个链接上找到了一个解决方案,它有效!

def myoverridenmeta(name, bases, adict):
    newClass = type(name, bases, adict)
    for field in newClass._meta.fields:
        if field.attname == 'last_login':
            field.column = 'lastlogin'
            field.db_column = 'lastlogin'
    return newClass

class UserProfile(AbstractBaseUser):

    class Meta:
        """
        meta class for user role, defining ordering and db table name
        """
        ordering = ["name"]
        db_table = 'user'

    username = models.CharField(max_length=254, unique=True)
    name = models.CharField(max_length=254, blank=True)
    email = models.EmailField(blank=True, unique=True)
    createdon = models.DateTimeField(auto_now_add=True)
    updatedon = models.DateTimeField(auto_now=True)
    is_active = models.BooleanField(default=True, db_column='status')
    is_admin = models.BooleanField(default=False, db_column='isadmin')
    is_staff = models.BooleanField(default=False, db_column='isstaff')
    is_superuser = models.BooleanField(default=False, db_column='issuperuser')

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

    objects = CustomUserManager()

    def get_full_name(self):
        return self.username

    def get_short_name(self):
        return self.username

    __metaclass__ = myoverridenmeta
于 2016-03-16T06:47:51.747 回答