Skip to content

Instantly share code, notes, and snippets.

@mrkschan
Created August 10, 2011 08:53
Show Gist options
  • Save mrkschan/1136402 to your computer and use it in GitHub Desktop.
Save mrkschan/1136402 to your computer and use it in GitHub Desktop.
class Tag(models.Model):
"""
A tag.
"""
name = models.CharField(_('name'), max_length=50, unique=True, db_index=True)
objects = TagManager()
class Meta:
ordering = ('name',)
verbose_name = _('tag')
verbose_name_plural = _('tags')
def __unicode__(self):
return self.name
class TaggedItem(models.Model):
"""
Holds the relationship between a tag and the item being tagged.
"""
tag = models.ForeignKey(Tag, verbose_name=_('tag'), related_name='items')
content_type = models.ForeignKey(ContentType, verbose_name=_('content type'))
object_id = models.PositiveIntegerField(_('object id'), db_index=True)
object = generic.GenericForeignKey('content_type', 'object_id')
objects = TaggedItemManager()
class Meta:
# Enforce unique tag association per object
unique_together = (('tag', 'content_type', 'object_id'),)
verbose_name = _('tagged item')
verbose_name_plural = _('tagged items')
def __unicode__(self):
return u'%s [%s]' % (self.object, self.tag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment