I have this model to save post from the users:
class Tag(models.Model):
name = models.CharField(max_length=255, unique=True)
def add_tags(obj_id, body):
object = Post.objects.get(id=obj_id)
tag_list = [Tag.objects.create(name=word) for word in body.split()]
for tag in tag_list:
object.tags.add(tag)
class Post(models.Model):
user = models.ForeignKey(User)
body = models.TextField()
tags = models.ManyToManyField(Tag, blank=True)
pub_date = models.DateTimeField(default=timezone.now)
activity = GenericRelation(Activity, related_query_name="posts")
def save(self, *args, **kwargs):
super(Post, self).save(*args, **kwargs)
if self.body:
body = self.body
obj_id = self.id
add_tags(obj_id, body)
So whenever a user post something, I would like to check if there's any hash-tag used inside the body. If there are tags, then fetch the tags inside the list.
But when I am posting, the tag objects are created, but they are not adding for the Post.tags fields.
post.body example = Check #from the
http://somesitedotcom/page#idtop #hell yeah!
What am I doing wrong here?