Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to acess Foreign key on django template languages from queryset values

Here is my model

class MainCategory(models.Model):
    name = models.CharField(max_length=100)

    def __str__(self) :
        return self.name
    
class Category(models.Model):
    main_category = models.ForeignKey(MainCategory, on_delete=models.CASCADE, related_name="maincategory")
    name = models.CharField(max_length=100)

    def __str__(self) :
        return self.name + "--" + self.main_category.name
    
class SubCategory(models.Model):
    category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name="category_name")
    name = models.CharField(max_length=100)

    def __str__(self) :
        return self.name

View

def Home(request):
    sliders = Slider.objects.all()
    banners = BannerArea.objects.all()
    main_category = MainCategory.objects.all()
    context = {
        'sliders' : sliders,
        'banners' : banners,
        'main_category' : main_category
    }
    return render(request, 'main/home.html', context)

I want to display Main Category and Sub category and category. Main Category is displayed but sub category and category are not displayed. Can you help me to fix this problem

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Header.html files

<div class="cat__menu">
                                <nav id="mobile-menu" style="display: block;">
                                    <ul>

                                        {% for i in main_category %}

                                        <li>
                                            <a href="shop.html"> {{ i.name }} <i class="far fa-angle-down"></i></a>
                                            <ul class="mega-menu">

                                                {% for cat in i.category_set.all %}

                                                <li><a href="shop.html">{{cat.name}}</a>
                                                    <ul class="mega-item">
                                                        {% for sub_cat in cat.subcategory_set.all %}
                                                        <li><a href="product-details.html">{{sub_cat.name}}</a></li>
                                                        {% endfor %}
                                                        
                                                    </ul>
                                                </li>
                                                {% endfor %}
  
                                            </ul>
                                        </li>

                                        {% endfor %}
                                        
                                    </ul>
                                </nav>
                            </div>
  

I want to display Main Category and Sub category and category. Main Category is displayed but sub category and category are not displayed. Can you help me to fix this problem

>Solution :

It is not working because you have defined related name for reverse relationship. Hence instead of <modelname>_set you should use that related name, like this:

 {% for i in main_category %}
    {% for cat in i.maincategory.all %}
         {{ cat }}
        {% for sub_cat in cat.subcategory_set.all %}
         {{ sub }}
        {% endfor %}
    {% endfor %}
  {% endfor %}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading