Search code examples
djangodjango-taggit

django-taggit: how to programmatically create tags?


For example:

tags_input = "hello, foo, bar, ok"
tagset = tag_parser(tags_input) # {'bar', 'foo', 'hello', 'ok'}

obj = Post(title=title, tags=tagset)
obj.save()

The above snippet doesn't seem to work. How can I create tags programmatically? e.g. in views


Solution

  • According to Taggit documentation:

    apple = Food.objects.create(name="apple")
    apple.tags.add("red", "green", "delicious")