#StackBounty: #django #postgresql #django-models #django-views #django-admin Approaches for adding multiple image on a post Django

Bounty: 50

I am working on a platform where I need to allow users to upload multiple images in one post.

  • I want to keep it as simple as possible, so that a person wouldn’t have to refresh the page to upload each image, or create and save a post before adding images.
  • If a user could delete or reorder images it would be nice

Can you give me advice on how to do it properly?

I am using django , postgres and here’s what I have done so far .

On my models.py –

class ImagePost(models.Model):
    user = models.ForeignKey("profiles.HNUsers", on_delete=models.DO_NOTHING)
    image = OptimizedImageField("Post Image", blank=True, null=True)
    timestamp = models.DateTimeField("Timestamp", blank=True, null=True, auto_now_add=True)
    text = models.TextField("Description text", blank=True)

    class Meta:
        verbose_name_plural = "Image Posts"

It can take one image just fine . what should I change to make it multi upload post

On my views.py this is what I have done so far –

def image_post(request):
    if request.method == 'POST':
        data = request.data
        serializer = ImagePostSerializer(data=data)
        if serializer.is_valid():
            print("image object saved")

                image_post_object = ImagePost.objects.filter(user__id=data['user']).order_by('-timestamp')[0]
                    post = Posts()

                    post.user = HNUsers.objects.get(id=data['user'])
                    post.image_url = image_post_object.image.url
                    post.type = 'I'
                    post.category = data['category']
                    post.created_on = image_post_object.timestamp
                    post.text = image_post_object.text
                    save_post = post.save()
                    post_id = post.pk

                        user = HNUsers.objects.get(pk=data['user'])
                        if user.user_type == 'HN':
                            payload = {
                                "user_name": user.full_name,
                                "image_url": user.profile_img_url,
                                "post_id": post_id,
                                "notification_type": "hn"
                            broadcast_notification('all', payload, 1)
                    except HNUsers.DoesNotExist:

                    print("post object created")

                except Posts.DoesNotExist:
                    print("Failed - post creation failed")

                except HNUsers.DoesNotExist:
                    print("Failed - user object not found")

            except ImagePost.DoesNotExist:
                print("Failed - image object not found")

    return Response(serializer.data, status=status.HTTP_200_OK)

Any advice is really appreciated.

Get this bounty!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.