如何使用 django-tagging 将非标签字段中的字符串作为标签包含在内?
在 Django 中,使用 django-tagging 应用程序,我希望将非 TagField 字段中包含的项目(例如本例中的 authors
)添加到列表中保存对象时显式提供标签。
class Publication(models.Model):
title = models.CharField(max_length=200)
authors = models.CharField(max_length=200)
tags = TagField()
如果提交的作者是“John,Bob,Mary”并且提交了标签“cooking,fun”,那么如何将最终标签设置为“John,Bob,Mary,cooking,fun”?
我尝试向 Publication 类添加自定义保存函数,但我认为我做得不对。
def save(self, *args, **kwargs):
super(Publication, self).save(*args, **kwargs)
for author in parse_tag_input(self.authors):
Tag.objects.add_tag(self, slugify(author))
super(Publication, self).save(*args, **kwargs)
我如何添加这些额外的标签?
In Django, using the django-tagging app, I want to make it so that items included in a field that isn't a TagField (e.g., authors
, in this example) are added to the list of explicitly provided tags when the object is saved.
class Publication(models.Model):
title = models.CharField(max_length=200)
authors = models.CharField(max_length=200)
tags = TagField()
If the authors that are submitted are "John, Bob, Mary" and the tags "cooking, fun" are submitted, how do I get the final tag set to be "John, Bob, Mary, cooking, fun"?
I tried adding a custom save function to the Publication class, but I don't think I got it right.
def save(self, *args, **kwargs):
super(Publication, self).save(*args, **kwargs)
for author in parse_tag_input(self.authors):
Tag.objects.add_tag(self, slugify(author))
super(Publication, self).save(*args, **kwargs)
How do I add in those extra tags?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为第二个 super(...).save(...) 是错误的。
根据文档
I think second super(...).save(...) is wrong.
You doing all right according docs