Question

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

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.

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.

Thanks in advance

Was it helpful?

Solution

I ran into a similar problem and forked django-taggit to add this functionality: 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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top