下面是编程之家 jb51.cc 通过网络收集整理的代码片段。
编程之家小编现在分享给大家,也给大家做个参考。
1
2 3 4 5 6 7 |
def htmlpath
(
self
):
paths = [ ] for p in self. path. split ( ':' ): c = ArticleCategory. objects. get (id__exact =p ) url = reverse ( 'cms.article.list' , kwargs = { 'cid':c. id } ) paths. append ( '<a href="%s" target="_blank">%s</a>' % (url , c. name ) ) return " > ". join (paths ) |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
from django.
db.
models.
signals
import pre_save
class ArticleCategory (models. Model ): name = models. CharField (max_length = 50 ) parent = models. ForeignKey ( 'self' , null = True , blank = True , related_name = 'children' ) path = models. CharField (max_length = 255 , null = True , blank = True ) def __unicode__ ( self ): if self. id == self. path: return self. name else: return self. node def _node ( self ): indent_num = len ( self. path. split ( ':' ) ) - 1 indent = '....' * indent_num node = u '%s%s' % (indent , self. name ) return node node = property (_node ) class Meta: ordering = [ 'path' ] #设置在model中的用途是,是在所有节点保存时递归的循环下去,更新所有的节点的路径 def save ( self , * args , ** kwargs ): super (ArticleCategory , self ). save (*args , ** kwargs ) if self. parent: self. path = '%s:%s' % ( self. parent. path , self. id ) else: self. path = self. id childrens = self. children. all ( ) if len (childrens ) > 0: for children in childrens: children. path = '%s:%s' % ( self. path , children. id ) children. save ( ) super (ArticleCategory , ** kwargs ) #信号触发,更新 def inital_articlecategory_path (sender , instance , **kwargs ): if instance. id: if instance. parent: instance. path = '%s:%s' % (instance. parent. path , instance. id ) else: instance. path = instance. id pre_save. connect (inital_articlecategory_path , sender =ArticleCategory ) |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class ArticleCategoryAdmin
(admin.
ModelAdmin
):
list_display = [ 'treenode' , 'patha' , 'id' , ] ordering = [ 'path' ] def patha ( self , obj ): if obj. parent: return u '%s > %s' % (obj. parent , obj. name ) return obj. name patha. short_description = 'path' patha. allow_tags = True def treenode ( self , obj ): indent_num = len (obj. path. split ( ':' ) ) - 1 p = '<div style="text-indent:%spx;">%s</div>' % (indent_num* 25 , obj. name ) return p treenode. short_description = 'tree path' treenode. allow_tags = True admin. site. register (ArticleCategory , ArticleCategoryAdmin ) |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
class ArticleCategory
(models.
Model
):
name = models. CharField (max_length = 50 ) parent = models. ForeignKey ( 'self' , ** kwargs ): #先保存数据,如果是新添加的数据,放在第一行是用来获得id,因为id是path的重要组成 super (ArticleCategory , ** kwargs ) if self. parent: self. path = '%s:%s' % ( self. parent. path , self. id ) else: self. path = self. id #更新完当前节点path后,要进行一次保存,否则在编辑类别时,子分类循环保存父类path不是最新的 super (ArticleCategory , ** kwargs ) childrens = self. children. all ( ) if len (childrens ) > 0: for children in childrens: children. path = '%s:%s' % ( self. path , children. id ) children. save ( ) |
以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。