[Answered ]-Shared Post Doesn't Post Images to the Shared Object In the backend

1👍

the error was that you are passing original_post to the GroupImage model instead of new_post.

Try to change this part

    if form.is_valid():
        new_post = Post(
            shared_body = request.POST.get('description'),
            description = original_post.description,
            username = original_post.username,
            date_posted = original_post.date_posted,
            group       = original_post.group,
            video      = original_post.video,
            shared_on = datetime.now(),
            shared_user = request.user)
        new_post.save()
        form = GroupImageForm(request.POST, request.FILES)
        if form.is_valid():
            new_post = form.save(commit=False)
            for image in original_post.groupimage_set.all():
                new_post = GroupImage(post=original_post, 
                                      group=original_post.group,
                                      shared_user=request.user,
                                      image=image.image)
                
                new_post.save()

to

    if form.is_valid():
        new_post1 = Post(
            shared_body = request.POST.get('description'),
            description = original_post.description,
            username = original_post.username,
            date_posted = original_post.date_posted,
            group       = original_post.group,
            video      = original_post.video,
            shared_on = datetime.now(),
            shared_user = request.user)
        new_post1.save()
        for image in original_post.groupimage_set.all():
            new_post = GroupImage(post=new_post1, 
                                      group=original_post.group,
                                      shared_user=request.user,
                                      image=image.image)
                
            new_post.save()
            print("new_post",new_post) # just for debugging the program...

I think the other way you can handle a share post is to have a foreignkey on the post itself.let me know if you need more help on this.

Leave a comment