1👍
tags
is a ManyToMany
field. You can’t use a direct assignment to a list item to update the field. You should instead assign via the field’s add
method tag objects corresponding to the items in the list.
Assuming you TagsExp
has a field label
which corresponds to the items in the list, you would do:
for tag_label in tags:
tag_label = tag_label.strip().lower() # clean up tag
tag, _ = TagsExp.objects.get_or_create(label=tag_label)
exp.tags.add(tag)
exp.save()
On another note, the tag in your traceback has a trailing space. That would create a new tag if there were no clean up as opposed to getting the existing one. Also, multiple cases will not be handled by default. I think you should give a look to django-taggit
app for tagging which has great features for managing whitespace characters and different cases.
Source:stackexchange.com