views:

108

answers:

2

I wanted to add a StackOverflow-style tag input to a blog model of mine. This is a model that has a lot of data already in it.

class BlogPost(models.Model):
    # my blog fields

try:
    tagging.register(BlogPost)
except tagging.AlreadyRegistered:
    pass

I thought that was all I needed so I went through my old database of blog posts (this is a newly ported blog) and copied the tags in. It worked and I could display tags and filter by tag.

However, I just wrote a new BlogPost and realise there's no tag field there.

Reading the documentation (coincidentally, dry enough to be used as an antiperspirant), I found the TagField. Thinking this would just be a manager-style layer over the existing tagging register, I added it. It complained about there not being a Tag column.

I'd rather not denormalise on tags just to satisfy create an interface for inputting them. Is there a TagManager class that I can just set on the model?

    tags = TagManager() # or somesuch
+2  A: 

Did you try using TagField() in the model instead of registering the model?

from tagging.fields import TagField

class BlogPost(models.Model):
    # ...
    tags = TagField()
istruble
I didn't do it instead, come to think of it, I did it *as well*. I'll check with the TagField but without the register.
Oli
Same error: no such column: post_blogpost.tags I know it just wants me to add a new char column to my database table... My point is, why should I? All the actual tags should be stored in the tagging app's models. I don't want to cruft up my model with hard dependencies like this.
Oli
I remember now, you feel strongly about turning normalization up to 11. Might be time to either give in and let django-tagging do what it wants or roll your own tagging library.
istruble
A: 

Like istruble said (sorry I can't comment above): Did you try using TagField() in the model instead of registering the model?

from tagging.fields import TagField

class BlogPost(models.Model):
    # ...
    tags = TagField()

But after that, you have to change your database table. I would recommend to make a backup of your database. Then run manage.py reset APPNAME check out how the table has changed. Restore the backup and try to alter the table, so that it looks like the new one. In this way you wont lose your data ;)

And remember, syncdb will not work since the table exist allready.

HWM-Rocker