My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
UsefulTips  
Useful tips for using the tagging application.
Featured
Updated Feb 4, 2010 by jonathan.buchanan

Useful Tips

Simplifying Tagging With Properties

Thanks to ubernostrum for this tip.

When using tags on a model, it's fairly easy to create a simple interface for retrieving and setting them by putting a property on the model:

def _get_tags(self):
    return Tag.objects.get_for_object(self)

def _set_tags(self, tag_list):
    Tag.objects.update_tags(self, tag_list)

tags = property(_get_tags, _set_tags)

So suppose you have a blog entry model with tags, and you add the above to it; now you could do:

e = Entry.objects.get(pk=12)
e.tags # prints the tag list
e.tags = 'foo bar' # now the Entry is tagged with 'foo' and 'bar'
Comment by ludvig.ericson, Aug 30, 2008

Why not make this an actual descriptor? Like,

class FooModel(models.Model):
    tags = TagManager()

It's not strictly a Django manager at that point, but it's a sort of manager.

Comment by m...@bigmir.net, Feb 11, 2009

# And why not to use simply:

from tagging.fields import TagField

class SomeModel(models.Model):
    tags = TagField()
Comment by peter.pi...@gmail.com, Mar 11, 2009

To get a list of the tags without hitting the database:

from tagging.utils import parse_tag_input

class Object(models.Model):
    tags = TagField()

    def get_tag_list(self):
        return parse_tag_input(self.tags)
Comment by mdorns...@gmail.com, Apr 18, 2010

To my understanding the discussion above is irrelevant with Version 0.3.

Comment by pclog...@gmail.com, Sep 15, 2011

Sign in to add a comment
Powered by Google Project Hosting