Home > Back-end >  Sub Category Name not coming despite the if condition coming as True
Sub Category Name not coming despite the if condition coming as True

Time:07-19

I am aware that I have asked this question before but I am really struggling and unable to solve this question

I have made product CRUD using serializers and foreign keys and I am making a dynamic page with categories and subcategories. The issue is that despite the if condition passing the sub categories are not getting printed only category_name is there as shown below models: enter image description here

class Products(models.Model):
    categories = models.ForeignKey(Category,on_delete=models.CASCADE)
    sub_categories = models.ForeignKey(SUBCategories,on_delete=models.CASCADE)
    color = models.ForeignKey(Colors,on_delete=models.CASCADE)
    size = models.ForeignKey(Size,on_delete=models.CASCADE)
    image = models.ImageField(upload_to = 'media/',width_field=None,height_field=None,null=True)
    title = models.CharField(max_length=70)
    price = models.CharField(max_length=10)
    sku_number = models.CharField(max_length=10)
    product_details = models.CharField(max_length=1000)
    quantity = models.IntegerField(default=0)
    isactive = models.BooleanField(default=True)

class Category(models.Model):
    #made changes to category_name for null and blank
    category_name = models.CharField(max_length=30)
    category_description = models.CharField(max_length=30)
    isactive = models.BooleanField(default=True)

class SUBCategories(models.Model):
    category_name = models.ForeignKey(Category, on_delete=models.CASCADE)
    sub_categories_name = models.CharField(max_length=30)
    sub_categories_description = models.CharField(max_length=30)
    isactive = models.BooleanField(default=True)

below is the shoppingpage.html

{% for result in category %}
                <div >
                    <div >
                        <div >
                            <a href="/9-6wear" target="_self">
                                <h4 >{{result.category_name}}</h4>
                            </a>
                            {% for ans in subcategory %}   
                                <ul >
                                    <li>
                                                                          
                                        {% if ans.category_name == result.category_name %}
                                            <a href="/kurta" target="_self">{{ans.sub_categories_name}}</a>
                                        {% endif %}
                                    </li>
                                </ul>
                            {% endfor %}                                      
                        </div>
                    </div>
                </div>  
{% endfor %}

Help is greatly appreciated thanks!

CodePudding user response:

Without seeing the view I can't be sure, but it seems like you are doing

ans.category_name == result.category_name

where ans is a SUBCategories object and result is a Category object. If this is the case, ans.category_name is a ForeignKey field and result.category_name is a CharField, so you probably dont want to compare them.

If you want to print all categories, and for each category print each sub category, I would do something like this:

Give your foreign key a name in models.py, also change the confusing field names. Also models should be singular, so change model name too:

class SubCategory(models.Model):
    category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='sub_categories')
    sub_category_name = models.CharField(max_length=30)
    sub_category_description = models.CharField(max_length=30)
    is_active = models.BooleanField(default=True)

Then make a view for it:

from django.views.generic import TemplateView

class YourView(TemplateView):
    template_name = '<your template name>'
    
    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)
        # use the foreign key name 'sub_categories' to prefetch related objects to reduce database queries
        context['categories'] = Category.objects.all().prefetch_related('sub_categories')
        return context

Then in your template you dont need the if check at all, as you will only get the sub categories related to the given category. We use a reverse lookup category.sub_categories.all to get the sub categories related to the category:

{% for category in categories %}
                <div >
                    <div >
                        <div >
                            <a href="/9-6wear" target="_self">
                                <h4 >{{category.category_name}}</h4>
                            </a>
                            {% for sub_category in category.sub_categories.all %}   
                                <ul >
                                    <li>                              
                                        <a href="/kurta" target="_self">{{sub_category.sub_category_name}}</a>
                                    </li>
                                </ul>
                            {% endfor %}                                      
                        </div>
                    </div>
                </div>  
{% endfor %}
  • Related