一、django tagging app安装:
pip install django-tagging
或者
easy_install django-tagging
二、在项目中的使用:
‘‘‘ 引入tagging相关模块 ‘‘‘
from tagging.fields import TagField
from tagging.models import Tag
class cases(models.Model):
name = models.CharField(u‘作品名‘, max_length=20)
tags = TagField()
def get_tags(self):
return Tag.objects.get_for_object(self)
def update_tags(self,tag_names):
Tag.objects.update_tags(self,tag_names)
def remove_all_tags(self):
Tag.objects.update_tags(self,None)
def __unicode__(self):
return u‘%s‘ % self.name
get_tags方法:获取该model的所有tags,以list的方式返回
update_tags方法:更新该model的所有tags,以list方式传入需要保存的所有tags
remove_all_tags方法:删除该model的所有tags(因为没有发现tagging更好的删除方法,暂用这个代替)
(这个只是初步的总结,后续可能继续更新;有问题欢迎留言交流)
原文:http://my.oschina.net/u/2371517/blog/527490