正如标题所示,我正在使用AUTH_USER_MODEL
使用AbstractUser
的现有表格。必须--fake
迁移。我必须手动添加到数据库中的任何其他列,然后在出现一些错误时添加到模型中。不理想。
无论如何,当我到达./manage.py createsuperuser
时,我收到与其不存在的字段相关的错误:is_superuser
,is_staff
等等。事情是表格中有字段这已经有了一个略有不同的名字。我可以改名。
但它让我想知道Django是否内置了将ORM字段名称转换为表字段名称的内容。类似的东西:
class Meta:
db_table = 'Users'
除非另有说明,否则Django将使用该名称。
我对文档的快速了解并没有立即产生任何结果。
https://docs.djangoproject.com/en/1.11/ref/models/options/
from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.
# Extend the User model
class User(AbstractUser):
id = models.AutoField(primary_key=True)
username = models.CharField(max_length=255, blank=False, null=False, unique=True)
first_name = models.CharField(max_length=255, blank=True, null=True)
last_name = models.CharField(max_length=255, blank=True, null=True)
email = models.CharField(max_length=255, blank=True, null=True)
phone = models.CharField(max_length=255, blank=True, null=True)
password = models.CharField(max_length=255, blank=True, null=True)
cono = models.IntegerField(blank=False, null=False)
ip_whitelist_1 = models.CharField(max_length=32, blank=True, null=True)
ip_whitelist_2 = models.CharField(max_length=32, blank=True, null=True)
ip_whitelist_3 = models.CharField(max_length=32, blank=True, null=True)
last_login = models.DateTimeField(blank=True, null=True)
password_changed = models.DateField(blank=True, null=True)
security_q1 = models.CharField(max_length=255, blank=True, null=True)
security_q2 = models.CharField(max_length=255, blank=True, null=True)
security_a1 = models.CharField(max_length=255, blank=True, null=True)
security_a2 = models.CharField(max_length=255, blank=True, null=True)
role = models.IntegerField(blank=True, null=True)
login_unlock_date = models.DateTimeField(blank=True, null=True)
challenge_unlock_date = models.DateTimeField(blank=True, null=True)
active = models.SmallIntegerField(blank=True, null=True)
updated = models.DateTimeField(blank=True, null=True)
created = models.DateTimeField(blank=True, null=True)
# having to add these
is_superuser = models.SmallIntegerField(blank=True, null=True)
is_staff = models.SmallIntegerField(blank=True, null=True)
is_active = models.SmallIntegerField(blank=True, null=True)
REQUIRED_FIELDS = []
USERNAME_FIELD = 'username'
is_anonymous = False
is_authenticated = True
class Meta:
db_table = 'Users'
答案 0 :(得分:1)
您可以尝试使用db-column选项:
# having to add these
is_superuser = models.SmallIntegerField(db_column="YOU_NAME1", blank=True, null=True)
# ^^^^^^
is_staff = models.SmallIntegerField(db_column="YOU_NAME2", blank=True, null=True)
# ^^^^^^
is_active = models.SmallIntegerField(db_column="YOU_NAME3", blank=True, null=True)
# ^^^^^^