简体   繁体   English

如何根据与另一个 model 的关系在模板视图中查询另一个 Django model

[英]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.

相关问题 如何在Django中查询其他模型未引用的模型 - How to query for Model in Django that is not referenced by another Model 与Django中另一个模型的查询集创建多对多关系 - Create many to many relation with query set of another model in Django 如何从一个 Django model 获取数据以在模板循环中查询另一个 model? - How to get data from one Django model to query another model within a template loop? 如何基于Django中的属性值将模型实例复制到另一个模型 - How to copy a model instance to another model based on attribute value in Django 如何根据Django中一个模型的计算重新计算另一个模型中的字段? - How to recalculate a field in another model based on the calculations of one model in Django? 如何基于另一个 model 的实例在 Django 中创建 model,但经过过滤 - How to create a model in Django based on the instance of another model, but filtered 基于另一个模型的Django模型选择 - Django Model choices based off another model 如何将模型的实例传递给 Django 中的另一个模型? - How to pass instance of a model to another model in Django? 如何基于另一个属性设置 Django model 属性? - How to set Django model attribute based on another attribute? Django如何将模型中的值添加到另一个应用程序的视图中? - Django how to add value from model into another application's view?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM