How to show child of parent in template?

Viewed 302

I have the following models:

class Category(models.Model):
    cat_image = models.ImageField(upload_to='Category',null=True, blank=True)
    cat_title = models.CharField(max_length=250)
    cat_overview = models.CharField(max_length=500)
    cat_slug = models.SlugField(unique=True)


class SubCategory(models.Model):
    sub_cat_image = models.ImageField(upload_to='SubCategory',null=True, blank=True)
    sub_cat_title = models.CharField(max_length=255)
    sub_cat_overview = models.CharField(max_length=500)
    sub_cat_parent = models.ForeignKey(Category, on_delete=models.CASCADE)
    sub_cat_slug = models.SlugField(unique=True)


class SubSubCategory(models.Model):
    subsub_cat_title = models.CharField(max_length=150)
    subsub_cat_parent = models.ForeignKey(SubCategory, on_delete = models.CASCADE)
    subsub_cat_slug = models.SlugField(unique=True)

I already filled these models with some data and I have no problem showing Category model in templates but there is a problem when I want to show SubCategory and SubSubCategory simultaneously in one template.

enter image description here

these are the categories, but when I click on "Python" for instance. This pages shows up:

enter image description here

the problem as you can see in above code is that only python syntax, Python Variables and Python loops belongs to Python Basic not Python Files and Python Logs. How do I avoid this and show only related SubSubCategory instances of SubCategory model?

Codes associated to above image:

views.py

def home(request):
    return render(request, 'tutorials/home.html', {'categories':Category.objects.all()})


def sub_cat(request, category):
    sub_cats = SubCategory.objects.filter(sub_cat_parent__cat_slug=category)
    subsub_cats = SubSubCategory.objects.filter(subsub_cat_parent__sub_cat_parent__cat_slug = category)

    context = {
        'sub_cats': sub_cats,
        'subsub_cats': subsub_cats,
    }
    return render(request, 'tutorials/sub_cat.html', context)

and sub_cat.html

<div class="row">
    <div class="col-md-3">
        <div class="sidenav">
                {% for sub_cat in sub_cats %}
                        <button class="dropdown-btn"> {{ sub_cat.sub_cat_title }} 
                                <i class="fa fa-caret-down"></i>
                        </button>
                        <div class="dropdown-container">
                                {% for subsub_cat in subsub_cats %}
                                        <a href="#">{{ subsub_cat.subsub_cat_title }}</a>
                                {% endfor %}
                        </div>
                {% endfor %}
        </div>
    </div>  
    <div class="col-md-9">
        <h2>Learn Python</h2>
        <p>
        Authoritatively ....
        </p>
</div>

Please help me with this thank you.

edit: If you know a better way other this method, so please help me.

1 Answers

Instead of getting all the SubSubCategory instances and using them twice you should use the reverse relation:

{% for subsub_cat in sub_cat.subsubcategory_set.all %}
    <a href="#">{{ subsub_cat.subsub_cat_title }}</a>
{% endfor %}

You can read more about reverse relation in the Related objects documentation.

Related