当前,我具有以下实体关系模型:
表关系
------------ ----------------- ------
| Objective | ------- | Objective_Task | ---------| Task |
------------ ----------------- ------
并且我正在尝试显示下表中的结果
| id | Task | Task status | Objective |
----------------------------------------------------------------
| 1 | Set up drivers | In progress | Set up my machine |
| 2 | Configure network | In progress | Set up the network |
| 3 | Test the network | In progress | Set up the network |
但是我得到的结果是:
| id | Task | Task status | Objective |
-----------------------------------------------------------------------
| 1 | Set up drivers | In progress | objective.Objective.None |
| 2 | Configure network | In progress | objective.Objective.None |
| 3 | Test the network | In progress | objective.Objective.None |
以下是针对该实体关系的类模型的设置:
目标模型
class Objective(models.Model):
id = models.AutoField(primary_key=True)
objective = models.CharField(null=False, max_length=60)
initial_date = models.DateField(default=timezone.now(), null=False)
expiration_date = models.DateField()
class Meta:
ordering = ['initial_date']
db_table = 'goal_objective'
任务模型
class Task(models.Model):
ACTIVE = 'Active'
FINALIZED = 'Finalized'
STATUS = [(ACTIVE, ACTIVE), (FINALIZED, FINALIZED)]
id = models.AutoField(primary_key=True)
objective = models.ManyToManyField(Objective)
task = models.CharField(null=False, max_length=140)
status = models.CharField(max_length=24, choices=STATUS, default=ACTIVE)
class Meta:
db_table = 'task_table'
这是我用于尝试填充表的代码。
objective_ids = []
qs_current_objectives = Objective.objects.filter(initial_date__gte=date(2020,1,1), expiration_date__lte=date(2020,12,31)).values('id').values_list('id')
for value in qs_current_objectives:
objective_ids.append(value)
form = {'task_list': Task.objects.filter(objective__in=objective_ids)}
retrieval.html
{% for task in task_list %}
<table>
<tbody>
<tr>
<td>{{task.id}}</td>
<td>{{task.task}}</td>
<td>{{task.status}}</td>
<td>{{task.objective}}</td>
</tr>
</tbody>
</table>
我在模型中错过了什么我无法达到目标的名称?
为了避免将来出现问题,您应该更改一些内容。
class Objective(models.Model):
initial_date = models.DateField(default=timezone.now, null=False)
请注意,您传递函数,而不是传递函数调用default
的结果。如果您执行了所有操作,则每次重新启动Web服务器时,默认值都会更改。将函数传递为默认值会将日期设置为当前日期。
class Task(models.Model):
objectives = models.ManyToManyField(Objective, related_name='tasks')
使用单词的复数形式命名关系使代码更易于理解。现在,当您访问task.objectives
时,您知道它是一个集合。 task.objective
似乎应该只有一个。
在您看来,您无需指定只需要ID。只要它是您要传递的Queryset的实例,Django就会弄清楚。它还只会进行一个查询来获取数据。
current_objectives = Objective.objects.filter(
initial_date__gte=date(2020,1,1),
expiration_date__lte=date(2020,12,31)
)
tasks = Task.objects.filter(objectives__in=current_objectives).distinct()
form = {'task_list': tasks}
或者您可以遍历过滤器中的关系。
tasks = Task.objects.filter(
objectives__initial_date__gte=date(2020,1,1),
objectives__expiration_date__lte=date(2020,12,31),
).distinct()
form = {'task_list': tasks}
然后您的模板应如下所示:
{% for task in task_list %}
<table>
<tbody>
<tr>
<td>{{task.id}}</td>
<td>{{task.task}}</td>
<td>{{task.status}}</td>
<td>{{task.objectives.all}}</td>
</tr>
</tbody>
</table>
{% endfor %}
task.objectives
(或您当前代码中的task.objective
)仅是对Manager实例的引用,而不是集合本身。您需要访问.all
才能获取实际数据。