简体   繁体   English

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

[英]django-taggit custom 'tag' model and request.user

I have a requirement to track when and by whom a tag was created and so have created a custom tag model using django-taggit like so 我需要跟踪创建标签的时间和对象,因此需要使用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")

I'm using django-autocomplete-light to create an autocomplete field for Topics in the admin and typing in a new Topic creates it on saving the Article form. 我正在使用django-autocomplete-light在管理员中为Topics创建一个自动完成字段,并输入新的Topic会在保存Article表单时创建它。

While I know I can get request.user in the admin form and pass it thru the save_model method - which is what I'm doing for the Article model - I can't figure out how to do so for the Topics model. 虽然我知道可以在admin表单中获取request.user并通过save_model方法传递它(这是我对Article模型所做的工作),但我不知道如何对Topics模型进行操作。

Thanks in advance 提前致谢

I ran into a similar problem and forked django-taggit to add this functionality: https://github.com/professorplumb/django-taggit 我遇到了类似的问题,并分叉了django-taggit以添加此功能: https : //github.com/professorplumb/django-taggit

You add attributes for a custom through or tag model like so: 您可以通过以下方式为自定义直通或标记模型添加属性:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM