Skip to content Skip to sidebar Skip to footer

How I Can Make A Copy From Both Blog And Comments In Django?

I want to make a copy from my blog object and its comment. i write some code and it works for blog instance but does not copy its comments. This is my model: class Blog(models.Mode

Solution 1:

You have to copy each comment manually:

def copy(self):
    blog = Blog.objects.get(pk=self.pk)
    comments = blog.comment_set.all()

    blog.pk = None
    blog.save()

    for comment in comments:
        comment.pk = None
        comment.blog = blog
        comment.save()

    return blog.id

Post a Comment for "How I Can Make A Copy From Both Blog And Comments In Django?"