With django-taggit, I can easily build new tags in hindsight. That’s my model:
from django.db import models
from taggit.managers import TaggableManager
class KeyWord(models.Model):
text = models.CharField(max_length=80)
tags = TaggableManager()
…
That’s the script which builds for every keyword a tag for each word in text
:
key_words = KeyWord.objects.all()
for key_word in key_words:
key_word.tags.clear()
for kw in key_word.text.split():
key_word.tags.add(kw)
Now I try to do the same for new keywords or keywords that have changed, inspired by the docs “Overriding predefined model methods”:
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
self.tags.clear()
for kw in self.text.split():
self.tags.add(kw)
The saving itself works, but there is no effect on the tags. I guess it has something to do with the fact that tags
is a Manager and not a Field, but I can’t figure out how this works properly. Do you have an idea?