0

My template used to show the data correctly before I changed my model to add the following OrgLevel prior to my custom User table.

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'

My Custom User model is defined as the following:

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

In my view I have the owner defined as:

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)

If I print owner with:

print (f"owner:{owner.formattedusername}")

It will print the owner correctly. However, if I print owner.coid it will print the output as:

owner:OrgLevel object

How can I get the owner.coid to print as the value instead of the object?

1 Answer 1

1

owner.coid.coid

The first coid refers to the OrgLevel object. The second coid refers to the coid field within that object.

I strongly suggest using different names for the fields. Instead of coid as a field in User I suggest using org or orglevel. As far as the second coid, I recommend sticking to the default automatic id primary key provided by Django, except if you need to maintain compatibility with a pre-Django database.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the quick response, i figured it was something simple like that. By adding the foreign key I essentially created an additional level on my User table. Unfortunately i'll be unable to rename the tables as they are pre-existing production database tables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.