0

The closest thing that I could find to my questions is this.

I have the following model:

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'})

I have the following in my views:

def viewSpecAndRot(request):
    specialties = Specialty.objects.all
    rotations = Specialty_Category_Map.objects.all
    return render(request, 'view_spec_rot.html', {'specialties': specialties, 'rotations': rotations, }, )

Lastly, Here is part of the template:

{% 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 %}

This would all work well under normal circumstances, but I have a few subcategories of the rotations themselves like the following:

rotation
---something
------something_low
---------something_lower

How would I access the lower 2 children in the template? I have already looked into using recursetree on each rotation.category but to no avail.

mjr
  • 97
  • 2
  • 11

0 Answers0