我有一个页面,该页面在框列表中显示项目的快照,每个框都有项目所有者,标题日期之类的东西,还有一个窗口,其中将更新分为7个类别,更新是在一个单独的表中。遍历项目很好,但是更新来自所有项目。如何过滤其他项目?
我可以发布代码,但是在过去的一个月中我已经完成了2到3次,但是没有任何响应,因此怀疑我的结构是完全错误的。
为您提供我所追求的示例,以便能够提取针对项目ID进行过滤的最新更新,然后更新类别。这可能吗?
编辑:添加模板和CSS,以便希望以图形方式查看我正在尝试执行的操作。
models.py:
from datetime import datetime
from django.conf import settings
from django.contrib.auth.models import Permission, User
from django.db import models
from django.urls import reverse
User = settings.AUTH_USER_MODEL
class Area(models.Model):
'''
Project area - Digital, Uplink, DTH VoD, Interactive, OTT Streaming,
OTT VoD, OTT Platform, Operational Improvement, Archive.
'''
name = models.CharField(max_length=45, unique=True)
code = models.CharField(max_length=45, unique=True)
def __str__(self):
return self.name
class Priority(models.Model):
'''
Project priority - Low, Medium, High - defualt set to Low
'''
priority = models.CharField(max_length=16, unique=True)
colour = models.CharField(max_length=7, unique=True)
def __str__(self):
return self.priority
class Project(models.Model):
'''
Main Project, serves the default Projects Portal window.
'''
date_published = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
area = models.ForeignKey(Area, on_delete=models.PROTECT)
title = models.CharField(max_length=128, unique=True)
summary = models.CharField(max_length=256)
others = models.CharField(max_length=128, blank=True)
deadline = models.DateField(null=True, blank=True)
priority = models.ForeignKey(Priority, on_delete=models.PROTECT)
closed = models.DateTimeField(null=True, blank=True)
def __str__(self):
return self.title
class UpdateCategory(models.Model):
'''
Updates are split into 6 categories that applies to each project
with 2 extra categories of Other Updates and Mitigating issues.
'''
name = models.CharField(max_length=24, unique=True)
def __str__(self):
return self.name
class Update(models.Model):
'''
Latest update must have a category from UpdateCategory
and be linked to a project, all updates are timestamped.
'''
project = models.ForeignKey(Project, on_delete=models.CASCADE)
category = models.ForeignKey(UpdateCategory, on_delete=models.PROTECT)
update = models.TextField(max_length=2048, blank=True)
added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.update
因此,每个项目都将其更新(更新为项目状态)分为7个不同的类别,因为每个项目都遵循相同的结构,最后一个类别“其他更新”是该项目的问题/阻止者的统称,一小块就能看到每个项目的最新更新,可以快速概览该项目的位置。在视觉上,每个项目都将显示为一个块,其中显示了每个类别的最新更新。我在想我需要设置一个从每个类别中选择最新更新的函数(一个空白或null值完全有效,应该只是空白或空白),但是我不知道如何将其过滤对于每个项目,因为此刻循环发生在模板内。
{% extends "project_portal/base.html" %}
{% block home %}
<div id="main">
<div id="content">
<hgroup>
<h1>OTT</h1>
<h2>Service Integration Development Project Portal</h2>
</hgroup
</div>
{% if object_list %}
{% for project in project_list %}
<div class="container">
<div class="box1">
<h4>{{ project.title }}</h4>
<p>Project Status</p>
<div class="square"></div>
</div>
<div class="box2">
<strong>{{ project.summary }}</strong>
</div>
<div class="box3">
<strong>{{ project.user }}</strong>
</div>
<div class="box4">
<table class="items">
<col width="160px">
<tr class="updates">
<!-- <td>Monitoring Status</td> -->
<td><a href="{{ project.id }}/update">Impact Scenario</a></td>
<td>✅</td>
</tr>
<tr class="updates">
<td><a href="{{ project.id }}/update">Support Model</a></td>
<td>✅</td>
</tr>
<tr class="updates">
<td><a href="{{ project.id }}/update">Monitoring Status</a></td>
<td>✅</td>
</tr>
<tr class="updates">
<td><a href="{{ project.id }}/update">Training</a></td>
<td>✅</td>
</tr>
<tr class="updates">
<td><a href="{{ project.id }}/update">Service Rehearsals</a></td>
<td></td>
</tr>
<tr class="updates">
<td><a href="{{ project.id }}/update">Documentation</a></td>
<td></td>
</tr>
<tr class="updates">
<td><a href="{{ project.id }}/update">Other Updates</a></td>
<td></td>
</tr>
</table>
</div>
<div class="box5">
{% for item in update_category %}
<table>
<tr>
<td>{{ update.id }}</td>
</tr>
</table>
{% endfor %}
</div>
<div class="box6">
<h4>Project Timeline</h4>
</div>
<div class="box7">TimeLine</div>
</div>
{% endfor %}
{% endif %}
</div>
{% endblock %}