向现有模型添加了标签,现在它的管理如何工作?
我想将 StackOverflow 风格的标签输入添加到我的博客模型中。这是一个已经包含大量数据的模型。
class BlogPost(models.Model):
# my blog fields
try:
tagging.register(BlogPost)
except tagging.AlreadyRegistered:
pass
我认为这就是我所需要的,所以我浏览了旧的博客文章数据库(这是一个新移植的博客)并复制了标签。它起作用了,我可以显示标签并按标签进行过滤。
然而,我刚刚写了一篇新的 BlogPost 并意识到那里没有标签字段。
阅读文档(巧合的是,足够干燥,可以用作止汗剂),我找到了 TagField。我认为这只是现有标记寄存器之上的一个管理器风格的层,所以我添加了它。它抱怨没有标签列。
我不想仅仅为了满足创建用于输入它们的界面而对标签进行非规范化。是否有可以在模型上设置的 TagManager 类?
tags = TagManager() # or somesuch
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试在模型中使用 TagField() 而不是注册模型?
Did you try using TagField() in the model instead of registering the model?
就像 istruble 所说(抱歉我无法在上面发表评论):
您是否尝试在模型中使用 TagField() 而不是注册模型?
但之后,您必须更改数据库表。我建议您备份数据库。然后运行
manage.py reset APPNAME
查看表的变化情况。恢复备份并尝试更改该表,使其看起来像新表。这样你就不会丢失数据;)记住,syncdb 将不起作用,因为表已经存在。
Like istruble said (sorry I can't comment above):
Did you try using TagField() in the model instead of registering the model?
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.