Как я могу настроить html-шаблон для отображения моих представлений и моделей OneToOne в Django?
Я новичок в Django и создаю практический проект для своего портфолио.
У меня есть 2 модели в Django, к которым будут иметь доступ пользователи/супервайзеры, а также общие представления администраторов/суперпользователей.
class Vacation(models.Model):
vacation_id = models.AutoField(primary_key=True)
request_date = models.DateField(auto_now_add=True)
requestor = models.ForeignKey("Employee",on_delete=models.SET_NULL,null=True)
start_date = models.DateField(null=True)
end_date = models.DateField(null=True)
objects = models.Manager()
def __str__(self):
return (f"{self.vacation_id}")
pass
class VacationApproval(models.Model):
is_approved = models.BooleanField(default=False)
approved_by = models.ForeignKey(Supervisor,on_delete=models.SET_NULL,blank=True,null=True)
approved_date = models.DateField(null=True, blank=True)
team = models.CharField(choices=TEAM_CHOICES, max_length=255)
vacation_id = models.OneToOneField("Vacation",on_delete=models.CASCADE,null=True)
objects=models.Manager()
Я пытаюсь перенести утверждение на HTML-шаблон.
<thead>
<tr>
<th>Vacation ID</th>
<th>Request Date</th>
<th>Requestor</th>
<th>Start Date</th>
<th>End Date</th>
<th>Status</th>
<th>Approved Date</th>
</tr>
</thead>
<tbody>
{% for vacation in vacations %}
<tr>
<td>{{ vacation.vacation_id}}</td>
<td>{{ vacation.request_date }}</td>
<td>{{ vacation.requestor }}</td>
<td>{{ vacation.start_date }}</td>
<td>{{ vacation.end_date }}</td>
<td><span class="tag tag-success">{{ vacation.vacation_id.vacation_approvals.is_approved }}</span></td>
<td>{{ approval.approved_date }}</td>
</tr>
{% endfor %}
Мое представление ниже будет назначать модели для рендеринга в моем html-шаблоне
class VacationListView(ListView):
template_name = "vacation_list.html"
context_object_name='vacations'
def get_queryset(self):
user = self.request.user
if user.is_superuser:
queryset = Vacation.objects.all()
print("Superuser")
print(queryset)
else:
queryset = Vacation.objects.filter(
requestor=user
)
return queryset
def get_context_data(self, **kwargs):
user = self.request.user
context = super(VacationListView,self).get_context_data(**kwargs)
if user.is_organizer:
queryset = VacationApproval.objects.filter(
organization=user.userprofile,
)
context['vacation_approvals'] = VacationApproval.objects.all()
print(context)
return context