How to create my own Tag model

281 views
Skip to first unread message

Dominique Guardiola Falco

unread,
Dec 10, 2011, 8:42:46 AM12/10/11
to django...@googlegroups.com
I'll try to explain my question the best I can :

I want to add some fields and methods to the Tag model :
example, I need this model to have a fkey to a Concept model, a language field, etc
the additional methods are also linked to the others app: def create_concept_from_tag(): ....

I tried to use multi-table inheritance ( class MyTag(Tag): ... )  but this is not a good solution, because when I call the tags.() manager, I only get Tag objects, not "MyTag"

What should I subclass to retain the maximum functionality from taggit and be able to do what I want ?
( I don't mind about where is the model, I can keep the Tag model if needed )

Dominique Guardiola Falco

unread,
Dec 10, 2011, 8:57:00 AM12/10/11
to django...@googlegroups.com
Found some related questions on the google groupe, here's where I am now :

from taggit.models import Tag, GenericTaggedItemBase

class LabelledItem(GenericTaggedItemBase):
    pass

class Label(Tag):
    language    = models.CharField(_(u'language'),max_length=10, choices=LANG_LABELS, default='fr')
    user        = models.ForeignKey(User,blank=True,null=True,verbose_name=_(u'django user'),editable=False)
    concept     = models.ForeignKey(Concept,blank=True,null=True,verbose_name=_(u'main concept'))
  
and on another model :

class BaseInitiative(models.Model):
    tags = TaggableManager(through=LabelledItem)

But this throws an error "LabelledItem has no field named 'tag'"

From what I understand, the "tag" field is only defined in TaggedItemBase, why should I define it if I want to stay with the GFK through model ?

Dominique Guardiola Falco

unread,
Dec 10, 2011, 9:42:21 AM12/10/11
to django...@googlegroups.com
Ok got it

If you're OK to accept some input on the documentation about this use case, I'll work on it
I think the custom tagging part of the doc should include an example for different scenarios : we're not all so fluent with django classes and subclasses (django lowered the barrier to oop web tools, but the download does not include computer lessons :)

john....@gmail.com

unread,
Mar 21, 2015, 3:06:19 AM3/21/15
to django...@googlegroups.com
I'm also new to Taggit and trying to figure out a way to work with this thing.

I think, if you are going to add some fields to the Tag model, such that the Tag itself contains name, slug, language, user, and concept, you should extends the Label class from TagBase instead of Tag.  Tag (taggit.models.Tag) is a solid class.  TagBase (taggit.models.TagBase) is a abstract class.

class Label(TagBase):
    language = models.CharField(_(u'language'),max_length=10, choices=LANG_LABELS, default='fr')
    user     = models.ForeignKey(User,blank=True,null=True,verbose_name=_(u'django user'),editable=False)
    concept  = models.ForeignKey(Concept,blank=True,null=True,verbose_name=_(u'main concept'))
   
    class Meta:
        verbose_name = _("Tag")
        verbose_name_plural = _("Tags")

Am I correct on this?  Please feel free to comment.  Thanks.
Reply all
Reply to author
Forward
0 new messages