Home > database >  Django models. How do I make "add new" filed in Django?
Django models. How do I make "add new" filed in Django?

Time:07-21

I am trying to make an OfferUp-like web app using Django Framework. Everything has been going great until I ran into a problem. How could I make it so that users can upload multiple pictures, instead of just one using the models.ImageField() function? You know? We might have users that only have 5 pictures to upload, while another user might have 8. How could I make it so that users can upload into the database as many pictures as they want?

CodePudding user response:

What I'm going to suggest isn't that much different from the comment above (i don't have enough reputation to make a comment), so I'm just going to add a code snippet:

    class Item(models.Model):
        name = models.TextField()

    class ItemImage(models.Model):
        name = models.TextField()
        item = models.ForeignKey(Item, on_delete=models.CASCADE)
        image = models.ImageField(upload_to='images/')
    

and say: If you have more than one model with many images, rather than repeating the code you can just make a model (class) that will be inherited as the foreign key.

  • Related