0

The thing is, I have a model named ProductCategory, this model may or may not have another model as a parent category. consider I have three Categories : grandParent -> parent -> child Currently when I click on child category the URL is /productCategory/child I want to include it's parents as well. so if user clicks on "child" category URL must look like this /grandParent/parent/child in case of parent category clicked it must generate /grandparent/parent and when User clicks on a product I want to concatenate it's URL with the category URL something like this: /grandparent/parent/child/product_slug so can you please help me with this?

class ProductCategory(models.Model):
'''
product category model
'''
parent_category = models.ForeignKey(
    'self',
    verbose_name=_('Parent Category'),
    related_name=('sub_categories'),
    blank=True,
    null=True,
    on_delete=models.CASCADE
)

@property
def is_last_child(self):
    '''
    check whether current category is a parent for another category
    '''
    return self.sub_categories.count() == 0

@property
def is_root_category(self):
    '''
    Check wheter current category has no parent
    '''
    return self.parent_category is None

def get_absolute_url(self):
    return reverse('productcategory', kwargs={'slug': self.slug})

Update : These are the urls from Urls.py

path('productcategory/<slug>/', ProductCategoryView.as_view(), name='productcategory'),
path('product/<str:slug>/', ProductView.as_view(), name='product'),
4

0 回答 0