>我需要跟踪标签的创建时间和创建者,因此使用 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 方法传递它 - 这就是我为文章模型所做的 - 但我无法弄清楚如何为主题模型做到这一点。
提前致谢
我遇到了类似的问题,并分叉了django-taggit以添加此功能:https://github.com/professorplumb/django-taggit
您可以为自定义直通或标记模型添加属性,如下所示:
article.topics.add('topic1', 'topic2', created_by=request.user)