我是Django的新手,目前正在使用版本1.11和Python 3.6。我在一个模板中有三个模型。
模型1是我的自定义用户,它有一个名为formattedusername的主键。
模型2是我的访问列表,它应该与ntname上的User创建1对多的关系。 Model 2有一个名为report_id的引用表ReportList的外键。
模型3是我的报告列表,其中report_id为主键。
我尝试创建一个views.py来链接一个模板的所有三个表。在我的模板中,我想显示我的访问列表模型中列出的当前用户所有report_names。然后显示当前用户无权访问的所有可用报告,以便报告列表中的报告不存在于访问列表中。
For my views.py I tried several variations of the following:
def applicationdetail(request, ntname):
user = get_object_or_404(User, pk=formattedusername)
applicationlist = QvReportList.objects.all()
applicationaccess = QVReportAccess.objects.filter(user=ntname)
context = {'user' : request.user, 'applicationdetail' : applicationaccess}
return render(request, 'accounts/profile.html', context=context)
class IndexView(User):
context_object_name = 'profile_list'
template_name = 'contacts/index.html'
queryset = User.objects.all()
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
context['users'] = User.objects.filter(formattedusername=formattedusername)
context['reportaccess'] = QVReportAccess.objects.filter(ntname=ntname)
context['reportlist'] = QvReportList.objects.all()
# And so on for more models
return context
我的模板用于获取用户信息和当前访问权限我使用以下内容,但由于某种原因,我无法让应用程序返回数据库中存在的正确数据(report_name),为了下一步,我希望所有报告都不在reportaccess中:
{% extends 'base.html' %}
{% block head %}
<title> Profile </title>
{% endblock %}
{% block body %}
<div class = "container">
<h2>{{ user.username }}</h2>
<ul>
<li>Email: {{ user.email }}</li>
<li>Employee First Name: {{ user.first_name }}</li>
<li>Employee Last Name: {{ user.last_name }}</li>
<li>Coid: {{ user.coid }}</li>
<li>Facility: {{ user.facility }}</li>
<li>Job Description: {{ user.jobdescription }}</li>
<li>Position Description: {{ user.positiondescription }}</li>
</ul>
</div>
<div class = "container">
<h2>Current Access Application List</h2>
<ul>
<li>Application Name: {% for app in applicationdetail %}
<input type="checkbox" name="report_name" value="{{ app.report_name }}" /> {{ app.report_name }}<br />
{% endfor %}
</li>
</ul>
</div>
{% endblock %}
下面附有我使用graphviz的模型
以下是型号代码:
from __future__ import unicode_literals
from django.utils import timezone
from django.contrib.auth.models import (AbstractBaseUser,PermissionsMixin)
from django.db import models
from django.conf import settings
from django.forms import ModelForm
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)
facility = models.CharField(max_length=140)
jobdescription = models.CharField(max_length=140)
positiondescription = models.CharField(max_length=140)
coid = models.CharField(max_length=5)
streetaddress = models.CharField(max_length=140)
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);
# REQUIRED_FIELDS = "username"
def __str__(self):
return "@{}".format(self.username)
class QvReportList(models.Model):
report_id = models.CharField(db_column='Report_ID',primary_key=True, max_length=100, serialize=False) # Field name made lowercase.
report_name = models.CharField(db_column='Report_Name', max_length=255, blank=True, null=True) # Field name made lowercase.
report_name_sc = models.CharField(db_column='Report_Name_SC', max_length=255, blank=True, null=True) # Field name made lowercase.
qv_filename = models.CharField(db_column='QV_FileName', max_length=255, blank=True, null=True) # Field name made lowercase.
report_access = models.CharField(db_column='Report_Access', max_length=20, blank=True, null=True) # Field name made lowercase.
report_group_id = models.IntegerField(db_column='Report_Group_ID', blank=True, null=True) # Field name made lowercase.
report_sub_group_id = models.IntegerField(db_column='Report_Sub_Group_ID', blank=True, null=True) # Field name made lowercase.
qv_dept_id = models.IntegerField(db_column='QV_Dept_ID', blank=True, null=True) # Field name made lowercase.
qv_dept_desc = models.CharField(db_column='QV_Dept_Desc', max_length=100, blank=True, null=True) # Field name made lowercase.
load_date = models.DateTimeField(db_column='Load_Date', blank=True, null=True) # Field name made lowercase.
approver_fname = models.CharField(db_column='Approver_FName', max_length=255, blank=True, null=True) # Field name made lowercase.
approver_lname = models.CharField(db_column='Approver_LName', max_length=255, blank=True, null=True) # Field name made lowercase.
approver_ntname = models.CharField(db_column='Approver_NTName', max_length=255, blank=True, null=True) # Field name made lowercase.
beg_date = models.DateTimeField(db_column='Beg_Date', blank=True, null=True) # Field name made lowercase.
end_date = models.DateTimeField(db_column='End_Date', blank=True, null=True) # Field name made lowercase.
active = models.IntegerField(db_column='Active', blank=True, null=True) # Field name made lowercase.
approval_id = models.IntegerField(db_column='Approval_ID', blank=True, null=True) # Field name made lowercase.
role_based_id = models.IntegerField(db_column='Role_Based_ID', blank=True, null=True) # Field name made lowercase.
class Meta:
managed = False
db_table = 'QV_Report_List'
class QVReportAccess(models.Model):
user_status = models.CharField(db_column='User_Status', max_length = 20) # Field name made lowercase.
ntname = models.OneToOneField(User,db_column='NTName', max_length=7,primary_key=True, serialize=False) # Field name made lowercase.
report_name = models.CharField(db_column='Report_Name', max_length=50, blank=True, null=True) # Field name made lowercase.
report_name_sc = models.CharField(db_column='Report_Name_SC', max_length=30, blank=True, null=True) # Field name made lowercase.
datareduce_report_code = models.IntegerField(db_column='DataReduce_Report_Code', blank=True, null=True) # Field name made lowercase.
role_based_id = models.IntegerField(db_column='Role_Based_ID', blank=True, null=True) # Field name made lowercase.
report_id = models.OneToOneField(QvReportList,db_column='Report_ID', max_length=100, blank=True, null=True) # Field name made lowercase.
report_group_id = models.IntegerField(db_column='Report_Group_ID', blank=True, null=True) # Field name made lowercase.
report_access = models.CharField(db_column='Report_Access', max_length=50, blank=True, null=True) # Field name made lowercase.
sr_datareduce_summary_code = models.CharField(db_column='SR_DataReduce_Summary_Code', max_length=10, blank=True, null=True) # Field name made lowercase.
sr_datareduce_patient_code = models.CharField(db_column='SR_DataReduce_Patient_Code', max_length=10, blank=True, null=True) # Field name made lowercase.
userid = models.IntegerField(db_column='UserID', blank=True, null=True) # Field name made lowercase.
user_group_id = models.IntegerField(db_column='User_Group_ID', blank=True, null=True) # Field name made lowercase.
access_level_id = models.IntegerField(db_column='Active', blank=True, null=True)
active = models.IntegerField(db_column='Access_Level_ID', blank=True, null=True)
qv_statusid = models.IntegerField(db_column='QV_StatusID', blank=True, null=True)
employee_status_id = models.IntegerField(db_column='Employee_Status_ID', blank = True, null = True)
new_user = models.IntegerField(db_column='New_User', blank = True, null = True)
new_access = models.IntegerField(db_column='New_Access', blank = True, null = True)
new_report = models.IntegerField(db_column='New_Report', blank = True, null = True)
changed_row = models.IntegerField(db_column='Changed_Row',blank = True, null = True)
last_changed_date = models.DateTimeField(db_column='Last_Changed_Date', blank=True, null=True) # Field name made lowercase.
access_beg_date = models.DateTimeField(db_column='Access_Beg_Date', blank=True, null=True) # Field name made lowercase.
access_end_date = models.DateTimeField(db_column='Access_End_Date', blank=True, null=True) # Field name made lowercase.
report_beg_date = models.DateTimeField(db_column='Report_Beg_Date', blank=True, null=True) # Field name made lowercase.
report_end_date = models.DateTimeField(db_column='Report_End_Date', blank=True, null=True) # Field name made lowercase.
qv_startdate = models.DateTimeField(db_column='QV_StartDate', blank=True, null=True) # Field name made lowercase.
load_date = models.DateTimeField(db_column='Load_Date', blank=True, null=True) # Field name made lowercase.
class Meta:
managed = False
db_table = 'QV_ReportAccess'
答案 0 :(得分:0)
问题是由于我的格式化用户名未定义。 L. Long的回应有助于澄清这个问题。