django-taggit自定义'tag'模型和request.user

时间:2013-10-14 10:41:33

标签: python django django-taggit django-autocomplete-light

我需要跟踪创建标记的时间和人员,因此使用django-taggit创建了自定义标记模型

class Topics(TagBase):
    featured = models.BooleanField(_('Featured'), default=False)

    created = models.DateTimeField(_('Creation date'), auto_now_add=True, editable=False)
    created_by = models.ForeignKey(User, related_name="topic_created_by")


class ArticleTopic(ItemBase):
    content_object = models.ForeignKey('Article')
    tag = models.ForeignKey(Topics, related_name="topic_items")


class Article(models.Model):   
    title = models.CharField(_('Title'), max_length=255)

    excerpt = models.TextField(_('Excerpt'))
    content = models.TextField(_('Content'), blank=True)

    topics = TaggableManager(through=ArticleTopic)

    created = models.DateTimeField(_('Creation date'), auto_now_add=True, editable=False)
    created_by = models.ForeignKey(User, related_name="article_created_by")

我正在使用django-autocomplete-light为管理员中的主题创建自动填充字段,输入新主题会在保存文章表单时创建它。

虽然我知道我可以在admin表单中获取request.user并通过save_model方法传递它 - 这就是我正在为Article模型做的事情 - 我无法弄清楚如何为主题模型这样做

提前致谢

1 个答案:

答案 0 :(得分:2)

我遇到了类似的问题并分叉了django-taggit来添加此功能:https://github.com/professorplumb/django-taggit

您可以为自定义直通或标记模型添加属性,如下所示:

article.topics.add('topic1', 'topic2', created_by=request.user)