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?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2B4-nGrj%3DoK2Vmj7%2BqiDMJie8rQGFbQsjbbDAmj3vKrLGYx5qA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to