无法显示 M2M 关系中的所有结果
Cannot display all the results in a M2M relationship
目前,我有以下实体关系模型:
Table关系
------------ ----------------- ------
| Objective | ------- | Objective_Task | ---------| Task |
------------ ----------------- ------
我正在尝试显示如下所示的结果 table
| 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 型号
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'
这是我用来填充 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>
我在我的模型中错过了什么,我无法找到 objective 的名字?
您应该更改一些内容以避免将来出现问题。
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
才能获取实际数据。
目前,我有以下实体关系模型:
Table关系
------------ ----------------- ------
| Objective | ------- | Objective_Task | ---------| Task |
------------ ----------------- ------
我正在尝试显示如下所示的结果 table
| 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 型号
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'
这是我用来填充 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>
我在我的模型中错过了什么,我无法找到 objective 的名字?
您应该更改一些内容以避免将来出现问题。
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
才能获取实际数据。