我想将Profile模型与OnetoOneFields类的AbstractUserModel中的User模型相关联。可能吗?或任何解决此问题的方法。这是我的models.py
from django.db import models
#from django.contrib.auth.models import User
from django.contrib.auth.models import (
AbstractBaseUser
)
class User(AbstractBaseUser):
email = models.EmailField(max_length=255, unique=True)
active = models.BooleanField(default=True)
staff = models.BooleanField(default=False)
admin = models.BooleanField(default=False)
timestamp = models.DateTimeField(auto_now_add=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
def __str__(self):
return self.email
def get_full_name(self):
return self.email
def get_short_name(self):
return self.email
@property
def is_staff(self):
return self.staff
@property
def is_admin(self):
return self.admin
@property
def is_active(self):
return self.active
class Profile(models.Model):
#HERE
user = models.OneToOneField(AbstractBaseUser, on_delete=models.CASCADE)
nama_lengkap = models.CharField(max_length=100, blank=True, null=True)
tgl_lahir = models.DateField(null=True, blank=True)
alamat = models.CharField(max_length=255)
foto_profil = models.ImageField(upload_to='foto_profil',blank=True)
jabatan = models.ForeignKey(Jabatan, on_delete=models.CASCADE)
def __str__(self):
return "{} - {}".format(self.user, self.nama_lengkap)
当我进行迁移时,只显示一些错误消息,如下所示:
SystemCheckError:系统检查确定了一些问题:
错误: users.Profile.user:(fields.E300)字段定义与模型“ AbstractBaseUser”的关系,该关系未安装或为抽象。 users.Profile.user:(fields.E307)字段users.Profile.user声明为对auth.abstractbaseuser的惰性引用,但应用程序auth不提供模型“ abstractbaseuser”。
预先感谢
答案 0 :(得分:0)
您需要替换此
user = models.OneToOneField(AbstractBaseUser, on_delete=models.CASCADE)
与
user = models.OneToOneField(User, on_delete=models.CASCADE)
答案 1 :(得分:0)
但是,我建议您以其他方式进行操作: 与其创建第二个用户模型,不如扩展现有模型:
#models.py
class ProfileUser(AbstractUser):
extra_field = models.CharField(max_length=100)
username = models.CharField(max_length=20) # modifing existing field
```
```
#settings.py
AUTH_USER_MODEL = "app_name.ProfileUser"