我能找到的最接近我问题的是this。
我有以下型号:
class Specialty(models.Model):
name = models.CharField(max_length=128)
class Meta:
verbose_name_plural = 'Specialties'
def __unicode__(self):
return self.name
class Category(MPTTModel):
name = models.CharField(max_length=128, unique=True)
parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True)
class Meta:
verbose_name_plural = 'Categories'
def __unicode__(self):
return self.name
class Specialty_Category_Map(models.Model):
specialty = models.ForeignKey(Specialty, on_delete=models.CASCADE)
category = models.ForeignKey(Category, on_delete = models.CASCADE, limit_choices_to={'parent__name':'rotation'})
我的观点中有以下内容:
def viewSpecAndRot(request):
specialties = Specialty.objects.all
rotations = Specialty_Category_Map.objects.all
return render(request, 'view_spec_rot.html', {'specialties': specialties, 'rotations': rotations, }, )
最后,以下是模板的一部分:
{% for specialty in specialties %}
<h3><a data-toggle="pill" href="#" class="">{{specialty}}</a></h3>
<div>
{% for rotation in rotations %}
{% ifequal specialty rotation.specialty %}
<li class="list-group-item ">{{rotation.category}}</li>
{% endifequal %}
{% endfor %}
</div>
{% endfor %}
在正常情况下,这一切都能很好地运作,但我有一些旋转子类别,如下所示:
rotation
---something
------something_low
---------something_lower
如何访问模板中的低2个孩子?我已经考虑在每个rotation.category
上使用recursetree ,但无济于事。