python - call model from within self model in django -


i want access self model within same model:
want , show parent of each category item(if exist)

class category(models.model):     title = models.charfield(...)     parent_id = models.foreignkey("category")     ref_id = models.charfield(...)      def __unicode__(self):         p1 = none         p2 = none         p3 = none         final = ''          p3 = self         if p3.parent_id:             p2 = self.__class__.objects.get(id=p3.parent_id)             if p2.parent_id:                 p1 = self.__class__.objects.get(id=p2.parent_id)                 final = smart_text('{}-{}-{}').format(p1.title,p2.title,p3.title)             else:                 final = smart_text('{}-{}').format(p2.title,p3.title)         else:             final = smart_text('{}').format(p3.title)          return final 

class category(models.model):      title = models.charfield(...)     parent_id = models.foreignkey('category',null=true,blank=true)     ref_id = models.charfield(...)      def __unicode__(self):     if self.parent_id:         return self.get_title()     return self.title      def get_title(self):         title = '%s-%s'%(self.title,self.parent_id.title)         parent = self.parent_id         while true:             if parent.parent_id:                 title+= '-%s'%parent.parent_id.title                 parent = parent.parent_id             break         return title 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -