我有以下自定义用户模型。它有一个字段代码,我想与我的设施维度表相关联。
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(unique=True)
username = models.CharField(max_length=7, unique=True)
formattedusername = models.CharField(max_length=11, unique=True, primary_key = True)
first_name = models.CharField(max_length=40)
last_name = models.CharField(max_length=140)
date_joined = models.DateTimeField(default=timezone.now)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_cfo = models.BooleanField(default=False)
facility = models.CharField(max_length=140)
officename = models.CharField(max_length=100)
jobdescription = models.CharField(max_length=140)
positioncode = models.CharField(max_length = 100)
positiondescription = models.CharField(max_length=140)
coid = models.CharField(max_length=5)
streetaddress = models.CharField(max_length=140)
title = models.CharField(max_length=100)
USERNAME_FIELD = 'username'
class Meta:
app_label = 'accounts'
db_table = "user"
def save(self, *args, **kwargs):
self.formattedusername = '{domain}\{username}'.format(
domain='HCA', username=self.username)
super(User, self).save(*args, **kwargs);
def get_short_name(self):
return self.username
我在Facility_Dimension表中定义了外键,但它正在尝试在formattedusername上创建关系。
class FacilityDimension(models.Model):
unit_num = models.CharField(db_column='Unit_Num', max_length=5, blank=True, null=True) # Field name made lowercase.
company_code = models.CharField(db_column='Company_Code', max_length=1, blank=True, null=True) # Field name made lowercase.
coid = models.OneToOneField(settings.AUTH_USER_MODEL,db_column='Coid',primary_key=True, serialize=False, max_length=5) # Field name made lowercase.
coid_name = models.CharField(db_column='COID_Name', max_length=50, blank=True, null=True) # Field name made lowercase.
c_level = models.CharField(db_column='C_Level', max_length=6, blank=True, null=True) # Field name made lowercase.
company_name = models.CharField(db_column='Company_Name', max_length=50, blank=True, null=True) # Field name made lowercase.
s_level = models.CharField(db_column='S_Level', max_length=6, blank=True, null=True) # Field name made lowercase.
sector_name = models.CharField(db_column='Sector_Name', max_length=50, blank=True, null=True) # Field name made lowercase.
b_level = models.CharField(db_column='B_Level', max_length=6, blank=True, null=True) # Field name made lowercase.
group_name = models.CharField(db_column='Group_Name', max_length=50, blank=True, null=True) # Field name made lowercase.
r_level = models.CharField(db_column='R_Level', max_length=6, blank=True, null=True) # Field name made lowercase.
division_name = models.CharField(db_column='Division_Name', max_length=50, blank=True, null=True) # Field name made lowercase.
d_level = models.CharField(db_column='D_Level', max_length=6, blank=True, null=True) # Field name made lowercase.
market_name = models.CharField(db_column='Market_Name', max_length=50, blank=True, null=True) # Field name made lowercase.
f_level = models.CharField(db_column='F_Level', max_length=6, blank=True, null=True) # Field name made lowercase.
cons_facility_name = models.CharField(db_column='Cons_Facility_Name', max_length=50, blank=True, null=True) # Field name made lowercase.
lob_code = models.CharField(db_column='LOB_Code', max_length=3, blank=True, null=True) # Field name made lowercase.
lob_name = models.CharField(db_column='LOB_Name', max_length=20, blank=True, null=True) # Field name made lowercase.
sub_lob_code = models.CharField(db_column='Sub_LOB_Code', max_length=3, blank=True, null=True) # Field name made lowercase.
sub_lob_name = models.CharField(db_column='Sub_LOB_Name', max_length=20, blank=True, null=True) # Field name made lowercase.
state_code = models.CharField(db_column='State_Code', max_length=2, blank=True, null=True) # Field name made lowercase.
pas_id_current = models.CharField(db_column='PAS_ID_Current', max_length=8, blank=True, null=True) # Field name made lowercase.
pas_current_name = models.CharField(db_column='PAS_Current_Name', max_length=40, blank=True, null=True) # Field name made lowercase.
pas_id_future = models.CharField(db_column='PAS_ID_Future', max_length=8, blank=True, null=True) # Field name made lowercase.
pas_future_name = models.CharField(db_column='PAS_Future_Name', max_length=40, blank=True, null=True) # Field name made lowercase.
summary_7_member_ind = models.CharField(db_column='Summary_7_Member_Ind', max_length=1, blank=True, null=True) # Field name made lowercase.
summary_8_member_ind = models.CharField(db_column='Summary_8_Member_Ind', max_length=1, blank=True, null=True) # Field name made lowercase.
summary_phys_svc_member_ind = models.CharField(db_column='Summary_Phys_Svc_Member_Ind', max_length=1, blank=True, null=True) # Field name made lowercase.
summary_asd_member_ind = models.CharField(db_column='Summary_ASD_Member_Ind', max_length=1, blank=True, null=True) # Field name made lowercase.
summary_imaging_member_ind = models.CharField(db_column='Summary_Imaging_Member_Ind', max_length=1, blank=True, null=True) # Field name made lowercase.
summary_oncology_member_ind = models.CharField(db_column='Summary_Oncology_Member_Ind', max_length=1, blank=True, null=True) # Field name made lowercase.
summary_cath_lab_member_ind = models.CharField(db_column='Summary_Cath_Lab_Member_Ind', max_length=1, blank=True, null=True) # Field name made lowercase.
summary_intl_member_ind = models.CharField(db_column='Summary_Intl_Member_Ind', max_length=1, blank=True, null=True) # Field name made lowercase.
summary_other_member_ind = models.CharField(db_column='Summary_Other_Member_Ind', max_length=1, blank=True, null=True) # Field name made lowercase.
pas_coid = models.CharField(db_column='PAS_COID', max_length=5, blank=True, null=True) # Field name made lowercase.
pas_status = models.CharField(db_column='PAS_Status', max_length=1, blank=True, null=True) # Field name made lowercase.
company_code_operations = models.CharField(db_column='Company_Code_Operations', max_length=3, blank=True, null=True) # Field name made lowercase.
osg_pas_ind = models.CharField(db_column='OSG_PAS_Ind', max_length=1, blank=True, null=True) # Field name made lowercase.
abs_facility_member_ind = models.CharField(db_column='ABS_Facility_Member_Ind', max_length=1, blank=True, null=True) # Field name made lowercase.
abl_facility_member_ind = models.CharField(db_column='ABL_Facility_Member_Ind', max_length=1, blank=True, null=True) # Field name made lowercase.
intl_pmis_member_ind = models.CharField(db_column='INTL_PMIS_Member_Ind', max_length=1, blank=True, null=True) # Field name made lowercase.
hsc_member_ind = models.CharField(db_column='HSC_Member_Ind', max_length=1, blank=True, null=True) # Field name made lowercase.
loaddate = models.DateTimeField(db_column='LoadDate', blank=True, null=True) # Field name made lowercase.
class Meta:
managed = False
db_table = 'Facility_Dimension'
除了这个模型关系之外,我的所有其他表格都与我的用户表格有关,这是我想要的。
我尝试在我的用户模型中添加ForeignKey关系,但由于它在FacilityDimension之前加载,因此无法识别我的FacilityDimension表
尝试在我的用户模型中运行makemigrations时,使用以下外键时出现以下错误:
NameError: name 'FacilityDimension' is not defined
coid = models.ForeignKey(FacilityDimension,max_length=5)
答案 0 :(得分:1)
您可以通过以下方法之一解决此问题:
导入相关模型先前用户类模型
ForeignKey
中的Stringfy类名称(主要用于循环导入时使用)
models.ForeignKey('myapp.FacilityDimension', max_length=5)
答案 1 :(得分:0)
我发现我可以使用related_name =' coid'在我的模型中。这解决了这个问题。