Saving tag list to a field

56 views
Skip to first unread message

istvan...@gmail.com

unread,
Mar 4, 2014, 4:38:56 AM3/4/14
to django...@googlegroups.com
Hello all,

I have a strange problem and I am stuck a little. I have a Content model with tagging set up, and it is working just fine. The content model is used at quite a few places throughout the application, and some of the pages/areas cannot be cached (and right now I would like to avoid low-level caching in the views). 

So I had this idea of saving the comma-separated list of tags in a model field as a string to avoid multiple database hits, but I've run into a problem. 

First I tried an m2m_changed signal handler, but no such signal is fired by django-taggit (any reason why not by the way?), so the only option I see is the model's .save() method:

class Content(models.Model):
    tag_list = models.CharField(verbose_name=u"List of tags as string", max_length=255, blank=True, default="")
    
    tags = TaggableManager(blank=True)

    def save(*args, **kwargs):
        # some other stuff happening
        super(Content, self).save(*args, **kwargs)
        self.tag_list = ", ".join(self.tags.names())
        super(Content, self).save(*args, **kwargs)

And it works just fine when I re-save a Content with existing tags  - but when I add/delete tags in the admin, the tag_list field will not pick up the changes on the first save. 

So, any idea how I can solve this problem?


Thanks,
  Istvan
Reply all
Reply to author
Forward
0 new messages