I am having a hard time sharing post with my function, My challenge is whenever i share a post the share images in the original post doesn't post the images to the new post image field, but rather it repost the images in the original share post field and so it makes it not possible to show the shared image! my question why am I getting this effect of post being reposted in the original image field and not the new shared post field! hoping for a solution ?
Here is my function for sharing post:
def share_post(request, pk):
original_post = Post.objects.prefetch_related('groupimage_set').get(pk=pk)
form = ShareForm(request.POST, request.FILES)
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()
return redirect('group:main',original_post.group.pk)
else:
form = GroupImageForm(request.POST, request.FILES)
ctx = {'form':form, 'original_post':original_post}
return render(request,'group/share_form.html', ctx)
Here is my model for images and post
class Post(models.Model):
group = models.ForeignKey(GroupEx, on_delete=models.CASCADE, related_name="group_post")
username = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE ,related_name="group_user")
description = models.TextField(max_length=500, blank=True)
video = models.FileField(upload_to="group_videos", blank=True)
shared_body = models.TextField(blank=True, null=True)
date_posted = models.DateTimeField(auto_now_add=True)
shared_on = models.DateTimeField(blank=True, null=True)
shared_user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True, blank=True, related_name=' ')
Group image model
class GroupImage(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE)
group = models.ForeignKey(GroupEx,on_delete=models.SET_NULL, null=True)
shared_user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True, blank=True, related_name=' ')
image = models.ImageField(upload_to='GroupImageLibrary/', blank=True, null=True)
CodePudding user response:
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.