1
votes

I have this model Item:

class Item(CustomBaseModel):
    name = models.CharField(max_length=100)

Each item belongs to a particular category which may be a sub-category of another category. In short, categories will look like following:

Men
    accessories
    clothing
        tops
        jeans
    sportswear
Women
    accessories
    clothing
        skirt
        pyjamas
    sportswear

So, root categories(men, women) will have the same sub categories.

I have read many resources and tried the following:

1st Attempt:

Categories table

id | name        | parent_id
 1 | Men         |
 2 | Women       | 
 3 | accessories | 1
 4 | clothing    | 1
 5 | tops        | 4

Problems with this approach:

  • I need accessories for both men and women. I will have to duplicate accessories here.

  • Making a query to this table will be difficult later on, in my opinion

2nd attempt:

I tried to use django-taggit. It worked just fine as tags.

Problem with this approach:

  • Tags are not categories. There is no category/subcategory, parent/child relationship with tags.

What is the best way to implement this category/subcategory relationship?

3
@madzohan, thanks. I will give it a tryRodrigue

3 Answers

2
votes

Just don't make Man and Woman a category. Accessories, clothing and sportswear are root categories.

For man and woman is the attribute of a product (may be for both—unisex—so make the sex multi value choice).

Then hardcode Man and Woman (or even Child) in the templates—it isn't likely there will be more sexes soon—and show only the categories which contain products for that sex.

1
votes

add a field

parent = models.ManyToManyField("self", null=True, blank=True)

-2
votes

I'm also trying to do something like this, find any solution?