djangodjango-formsimagefielddjango-filebrowser

Django, select image from previous uploaded images


I have a Post model which has a ImageField.

class Post(models.Model):
    headline = models.CharField(max_length=255)
    lead = models.TextField(blank=True, null=True)
    body = models.TextField(blank=True, null=True)
    image = models.ImageField(upload_to="posts", blank=True, null=True, max_length=255)

In my ModelForm, the file input for the image works perfectly, but I was wondering how to let the user choose between uploading a new image, or selecting one from all the images that have been previously uploaded to the site.

Any pointers?


Solution

  • In order to do that define another model Image and then create a foreign key from Post to Image. When creating a new post, you can either select an existing image or upload a new one and then select it.