The simplest way is to remove all the tags from pub.tags
first, then add them all back in:
# Clear the existing tags
pub.tags.clear()
tags = tags.split(',')
for tag in tags:
obj, create = Tag.objects.get_or_create(name=tag)
pub.tags.add(obj)
Instead of looping and building a String, you might try this more pythonic method:
tags = ['red', 'green', 'blue'] ','.join(tags) 'red,green,blue'