簡體   English   中英

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

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

我需要跟蹤創建標簽的時間和對象,因此需要使用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在管理員中為Topics創建一個自動完成字段,並輸入新的Topic會在保存Article表單時創建它。

雖然我知道可以在admin表單中獲取request.user並通過save_model方法傳遞它(這是我對Article模型所做的工作),但我不知道如何對Topics模型進行操作。

提前致謝

我遇到了類似的問題,並分叉了django-taggit以添加此功能: https : //github.com/professorplumb/django-taggit

您可以通過以下方式為自定義直通或標記模型添加屬性:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM