我的模板用于在我更改模型之前正确显示数据,以便在我的自定义用户表之前添加以下OrgLevel。
class OrgLevel(models.Model):
coid = models.CharField(db_column='Coid', max_length=5, primary_key = True, unique = True) # Field name made lowercase.
slevel = models.CharField(db_column='SLevel', max_length=6) # Field name made lowercase.
blevel = models.CharField(db_column='BLevel', max_length=6) # Field name made lowercase.
rlevel = models.CharField(db_column='RLevel', max_length=6) # Field name made lowercase.
dlevel = models.CharField(db_column='DLevel', max_length=6) # Field name made lowercase.
class Meta:
managed = False
db_table = 'OrgLevel'
我的自定义用户模型定义如下:
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.OneToOneField(OrgLevel, null=True, blank = True)
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
在我看来,我将所有者定义为:
def profile(request):
owner = User.objects.get (formattedusername=request.user.formattedusername)
reportdetail = QVReportAccess.objects.filter(ntname = owner.formattedusername, active = 1).values('report_name_sc')
reportIds = QVReportAccess.objects.filter(ntname = owner.formattedusername).values_list('report_id', flat=True)
reportaccess = QvReportList.objects.filter(report_id__in= reportIds).values_list('report_name_sc', flat = True).distinct()
reportGroups = QVReportAccess.objects.filter(ntname = owner.formattedusername).values_list('report_group_id', flat=True)
reportlist = QvReportList.objects.filter(~Q(report_id__in= reportIds)).exclude(active=0)
allreports = 'placeholder'
if allreports in reportaccess:
bhreportgrouplist = None
cereportgrouplist = None
finreportgrouplist = None
careportgrouplist = None
pireportgrouplist = None
screportgrouplist = None
dssreportgrouplist = None
psgreportgrouplist = None
othreportgrouplist = None
showbutton = None
else:
bhreportgrouplist = QvReportList.objects.filter(~Q(report_id__in= reportIds)).filter(report_group_id = 200)
cereportgrouplist = QvReportList.objects.filter(~Q(report_id__in= reportIds)).filter(report_group_id = 500)
finreportgrouplist = QvReportList.objects.filter(~Q(report_id__in= reportIds)).filter(report_group_id = 600)
careportgrouplist = QvReportList.objects.filter(~Q(report_id__in= reportIds)).filter(report_group_id = 800)
pireportgrouplist = QvReportList.objects.filter(~Q(report_id__in= reportIds)).filter(report_group_id = 1100)
screportgrouplist = QvReportList.objects.filter(~Q(report_id__in= reportIds)).filter(report_group_id = 1200)
dssreportgrouplist = QvReportList.objects.filter(~Q(report_id__in= reportIds)).filter(report_group_id = 1300)
psgreportgrouplist = QvReportList.objects.filter(~Q(report_id__in= reportIds)).filter(report_group_id = 1400)
othreportgrouplist = QvReportList.objects.filter(~Q(report_id__in= reportIds)).filter(report_group_id = 99999)
showbutton = ""
print (f"owner:{owner.coid}")
print (f"reportdetail:{reportdetail}")
print (f"reportids:{reportIds}")
print (f"reportaccess:{reportaccess}")
print (f"reportgroups:{reportGroups}")
print (f"reportlist:{reportlist}")
args = {'user':owner, 'applicationaccess':reportaccess, 'applicationlist':reportlist, 'bhgrouplist':bhreportgrouplist, 'cegrouplist':cereportgrouplist, 'fingrouplist':finreportgrouplist
, 'cagrouplist':careportgrouplist, 'pigrouplist':pireportgrouplist, 'scgrouplist':screportgrouplist, 'dssgrouplist':dssreportgrouplist, 'psggrouplist':psgreportgrouplist
, 'othgrouplist':othreportgrouplist, 'showbutton':showbutton}
return render(request, 'accounts/profile.html', args)
如果我打印所有者:
print (f"owner:{owner.formattedusername}")
它会正确打印所有者。但是,如果我打印owner.coid,它将输出打印为:
owner:OrgLevel object
如何将owner.coid打印为值而不是对象?
答案 0 :(得分:1)
owner.coid.coid
第一个coid
引用OrgLevel对象。第二个coid
引用该对象中的coid
字段。
我强烈建议在字段中使用不同的名称。我建议使用coid
或org
,而不是orglevel
作为用户中的字段。至于第二个coid
,我建议坚持使用Django提供的默认自动id
主键,除非你需要保持与Django之前数据库的兼容性。