Krystian Kazimierczak
Krystian Kazimierczak

Reputation: 582

Subcategories Django Shop

I have 2 categories Man and Woman and I want to create subcategories and connect it in product This is my models.py

class Category(models.Model):
    name = models.CharField(max_length=200,
                            db_index=True)
    slug = models.SlugField(max_length=200,
                            db_index=True,
                            unique=True)

    class Meta:
        ordering = ('name',)
        verbose_name = 'category'
        verbose_name_plural = 'categories'

    def __str__(self):
        return self.name


class Product(models.Model):
    category = models.ForeignKey(Category,
                                 related_name='products')
    name = models.CharField(max_length=200, db_index=True)
    slug = models.SlugField(max_length=200, db_index=True)
    image = models.ImageField(upload_to='products/%Y/%m/%d',
                              blank=True)
    description = models.TextField(blank=True)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    stock = models.PositiveIntegerField()
    available = models.BooleanField(default=True)
    created = models.DateField(auto_now_add=True)
    update = models.DateField(auto_now=True)

    class Meta:
        ordering = ('name',)
        index_together = (('id', 'slug'),)

    def __str__(self):
        return self.name

I think I can create this class

class Subcategory 
    name = models.CharField(max_length=200,
                            db_index=True)
    slug = models.SlugField(max_length=200,
                            db_index=True,
                            unique=True)
   category = models.ForeignKey(Category)

and add it to Product class

 subcategory = models.ForeignKey(Subcategory) 

but i don't think is a good way

Upvotes: 1

Views: 237

Answers (2)

Dmitry Shilyaev
Dmitry Shilyaev

Reputation: 733

You can create adjacency tree from Category objects by using this field (as mentioned by voodoo-burger), i also added blank=True, to be able to create root Category in Django admin.

parent=models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True)

Next, if you have many categories and want to do queries on your tree structure, you need to look on MPTT (https://github.com/django-mptt/django-mptt).

Upvotes: 0

voodoo-burger
voodoo-burger

Reputation: 2153

You can add a parent_category field to your Category model which is a foreign key to itself:

parent_category=models.ForeignKey('self', on_delete=models.CASCADE, null=True)

Then you can treat any Category that has a parent_category as a subcategory.

If you want a product to be in more than one (sub)category you will have to make the category field on your Product model a ManyToMany field instead of a Foreign Key. If a product can have at most one category and at most one subcategory then you can leave a category and subcategory field on your Product model but you will have to set a related_name for each.

Upvotes: 2

Related Questions