0

This is my models in Django:

from django.db import models
# Create your models here.
statusChoice = ((1, 'Show'), (2, 'Hide'))

class newCatagory(models.Model):
    cat_name = models.CharField(max_length = 255)
    parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank = True)
    created_at = models.DateTimeField(auto_now_add=True)
    status = models.SmallIntegerField(choices = statusChoice, null=True)

    def __str__(self):
        return "%s, --%s" % (self.parent, self.cat_name)

class products(models.Model):
    pro_name = models.CharField(max_length = 255, null =True)
    sub_cat_id = models.ForeignKey(newCatagory, on_delete=models.CASCADE, null=True, blank = True)
    pro_image = models.ImageField(null = True)
    pro_price = models.FloatField(null = True)
    description = models.TextField(null = True)
    status = models.SmallIntegerField(choices = statusChoice, null=True)
    created_at = models.DateTimeField(auto_now_add=False, null=True)

    def __str__(self):
        return self.pro_name

i am creating models like above. But i don't know how to show my catagory to my view. How can i do that

This is my templates html:

<li><a href="#">{{This is catagory}}</a>
    <ul class="sub-menu">
        <li><a href="#">{{this is subcatagory}}</a></li>
    </ul>
</li>

I am trying to use for loop but don't know how to use, please help

alfredo138923
  • 1,509
  • 1
  • 15
  • 15
San San
  • 159
  • 2
  • 6

1 Answers1

0

this is a way of doing that:

statusChoice = ((1, 'Show'), (2, 'Hide'))

class newCatagory(models.Model):
    cat_name = models.CharField(max_length = 255)
    parent = models.ForeignKey('self', related_name='subcats', on_delete=models.CASCADE, null=True, blank = True)
    created_at = models.DateTimeField(auto_now_add=True)
    status = models.SmallIntegerField(choices = statusChoice, null=True)

    def __str__(self):
        return "%s, --%s" % (self.parent, self.cat_name)

then in the template:

{% for cat in categories %}
    {% if not cat.parent %}
        <li><a href="#">{{ cat }}</a>
            {% if cat.subcats.count %}
                <ul>
                {% for subcat in cat.subcats.all %}
                    <li> <a> {% subcat %} </a> </li>
                    {# if you already seted a `related_name` for product foreign_key to categuries then it will looklike this #}
                    {% if subcat.products.count %}
                        <ul>
                        {% for product in subcat.products.all %}
                            <li> <a href="{{ product.get_absolut_url }}"> {{ product.name }} </a> </li>
                        {% enfor %}
                        </ul>
                    {% endif %}
                {% enfor %}
                </ul>
             {% endif %}
        </li>
    {% endif %}
{% enfor %}

but if you want to go deep sub-category-ing , maybe its better to create a father Category class and then create one or more child classes to inherit from that father class,

class Products -> class DigitalProducts(Products) -> class MobilePhones(DigitalProducts) -> ...
mh-firouzjah
  • 834
  • 1
  • 6
  • 15