我有以下课程,我可以通过{{plantpart.fraction_id.fraction}}
上的模型来获取相关的对象,但我想采用另一种方法。我知道我需要一个related_name来避免_set
结尾,并且它必须位于嵌套的for循环中。
那么我该如何纠正以下问题:
{% for f in fractions %}
{% for pp in plantparts%}
{{pp.plantparts.fraction}}
{% endfor %}
{% endfor %}
class Fraction(models.Model):
fraction_id = models.AutoField(primary_key=True)
composition_id = models.ForeignKey(Composition, db_column='composition_id', on_delete = models.PROTECT)
fraction = models.CharField(max_length=20, choices = FRACTIONS)
class PlantPart(models.Model):
plantpart_id = models.AutoField(primary_key=True)
fraction_id = models.ForeignKey(Fraction, db_column='fraction_id', on_delete = models.PROTECT, related_name='plantparts')
taxon_id = models.ForeignKey(Taxon, db_column='taxon_id', on_delete = models.PROTECT, related_name='plant_taxon', blank=True, null=True)
part = models.CharField(max_length=100)
weight = models.DecimalField(max_digits=10, decimal_places=3)
quantity = models.DecimalField(max_digits=10, decimal_places=3)
p.s。我知道我不需要使用ID,但这可以连接到现有数据库。