[英]How to query another Django model in template view based on the relation to another model
I have these models我有这些模型
class Protocol(models.Model):
name = models.CharField(max_length=200)
class Description(models.Model):
name = models.CharField(max_length=200)
protocol = models.ForeignKey(Protocol)
class Trait(models.Model):
desc = models.CharField(max_length=200)
protocol = models.ForeignKey(Protocol)
class State(models.Model):
desc = models.CharField(max_length=200)
trait = models.ForeignKey(Trait)
class Expression(models.Model):
state = models.ForeignKey(State)
description = models.ForeignKey(Description)
So a Protocol (eg protocol "A") consist of a certain amount of Traits (eg height, weight, color).所以一个协议(例如协议“A”)由一定数量的特征(例如身高、体重、颜色)组成。 Each Trait can have multiple States (eg low, medium, high).每个特征可以有多个状态(例如低、中、高)。 Then, a Description is a collection of Expressions related to a specific Protocol .然后,描述是与特定协议相关的表达式的集合。 For example Description #1 is made with Protocol "A" and consist of two Expressions: height -> low, weight -> high;例如,描述 #1 由协议“A”组成,由两个表达式组成:身高 -> 低,体重 -> 高; but Trait color is not specified.但未指定特征颜色。
Basically, what i want to do is display in a template view all the Traits linked to the specific Protocol of the selected Description, and then the corresponding Expression which can also be empty for some Trait .基本上,我想做的是在模板视图中显示链接到所选描述的特定协议的所有特征,然后显示相应的表达式,对于某些特征也可以为空。
Like this:像这样:
| Trait | Expression |
|--------+------------|
| height | |
| weight | high |
| color | blue |
Using the shell i can easily return what i need使用 shell 我可以轻松返回我需要的东西
# Select a Description
desc = Description.objects.first()
# Get the protocol
protocol = desc.protocol
# Get all traits in the selected protocol
all_traits = protocol.trait_set.all()
# Get all the expressions in this description
expressions = desc.expression_set.all()
# Print all traits within the selected protocol and their related expression (if any)
for trait in all_traits:
print(trait.desc)
expr = expressions.filter(state__trait_id = trait.id).first()
print(expr.state.desc)
However, I can't figure out how to do it in a view, especially I would like continue using a DetailView, but it's not a strict requirement if not possible.但是,我无法弄清楚如何在视图中执行此操作,尤其是我想继续使用 DetailView,但如果不可能,这不是严格要求。 Is there any way?有什么办法吗?
Thanks谢谢
I found a way to do this, even though I'm not completely satisfied because I feel it is not a very elegant solution.我找到了一种方法来做到这一点,尽管我并不完全满意,因为我觉得这不是一个非常优雅的解决方案。
I created a description_extras.py
in a templatetags/
directory inside my app (do not forget the __init__.py
to treat it as a package)我在我的应用程序内的templatetags/
目录中创建了一个description_extras.py
(不要忘记将__init__.py
视为一个包)
Inside description_extras.py
I've created a custom filter, like this:在description_extras.py
,我创建了一个自定义过滤器,如下所示:
from django import template
register = template.Library()
@register.filter
def expr_trait(expression,trait_id):
return expression.filter(state__trait_id = trait_id).first()
Then in the template, I load the custom filter with然后在模板中,我加载自定义过滤器
{% load description_extras %}
And then I can call the filter like this:然后我可以这样调用过滤器:
{% for trait in description.protocol.trait_set.all %}
<tr>
<td>{{ trait.desc }}</td>
<td>{{ description.expression_set.all|expr_trait:trait.id }}</td>
</tr>
{% endfor %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.