I am getting the category id for GET request but I want to show text in hierarchical structure like child1 → child → child3
my expected response will be look like this
{
'category': 'child1 -> child -> child3'
}
now getting response like this
{
'category': 1
}
somethings look like that my model where it show category name on hierarchical order
my category_model
class Product_category(models.Model):
domain = models.CharField(max_length=250)
parent_category = models.ForeignKey('self',on_delete=models.CASCADE,null=True, blank=True)
category_name = models.CharField(max_length=200,null=True,blank=True,)
category_icon = models.ImageField(upload_to=dynamic_icon_upload_path,blank=True,null=True)
def get_hierarchical_name(self):
if self.parent_category:
return f"{self.parent_category.get_hierarchical_name()}->{self.category_name}"
else:
return f"{self.category_name}"
def __str__(self):
return self.get_hierarchical_name()
my product model
class Product(models.Model):
title = models.CharField(max_length=200,blank=True,null=True)
category = models.ForeignKey(Product_category,blank=True,null=True,on_delete=models.CASCADE)
my serilizer
class ParentProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
fields = ['category',...others product fields]
see the screenshot where right now I am getting id instated for hierarchical text