我有一个表(关系),该表将另一个表(上下文)中的两行关联起来。
这使我可以获得上下文和它们之间的关系:
即
上下文1-高于上下文2。
上下文2-高于上下文3。
如何反转返回模板的逻辑:
上下文2-是否低于上下文1?
遵循我的模型和模板。
models.py
class Context(models.Model):
context_id = models.AutoField(primary_key=True)
class Relationship (models.Model):
relationship_id = models.AutoField(primary_key=True)
context_id1 = models.ForeignKey(Context, db_column='context_id1', on_delete = models.PROTECT, related_name='relations_through1')
context_id2 = models.ForeignKey(Context, db_column='context_id2', on_delete = models.PROTECT, related_name='relations_through2')
relationship = models.CharField(max_length = 50, blank=True, null=True)
模板
{% for relationship in context.relations_through1.all %}
Context Above: {{________________________________}} <br>
Current Context:{{relationship.context_id1.number}} <br>
Relationship:{{relationship.relationship}} <br>
Context Below: {{relationship.context_id2.number}}
{% endfor %}
p.s。我知道_id后缀,Django已设置为不管理数据库。