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

标签 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 在管理中为主题创建一个自动完成字段,并在保存文章表单时输入新主题来创建它。

虽然我知道我可以在管理表单中获取 request.user 并将其通过 save_model 方法传递 - 这就是我为 Article 模型所做的事情 - 我不知道如何为 Topics 模型执行此操作.

提前致谢

最佳答案

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

您可以为自定义的 through 或 tag 模型添加属性,如下所示:

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

关于python - django-taggit 自定义 'tag' 模型和 request.user,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19358431/

相关文章:

python - 递归函数在 Python 中不返回任何内容

python - 如何在 python ax.scatter 3D plot 中连接点

python - NURBS 曲面到水密 STL

python - 如何在 Django 中将 S3 文件附加到电子邮件

python - Django 在模板中仅迭代查询集 n 次

django - 如何使用 TaggableManager 字段为模型创建 Factory-Boy 工厂

python - wxpython GridBagSizer问题

python - 在我的网络应用程序上使用 ubuntu 字体

django - 链接到 django 管理站点

Django __小写