[Fixed]-Django – uploading several images at the time

1👍

Since I’m assuming you’re using a relational database and your model field name is ‘photos’ you’ll want more than one photo per location.

You can do something like:

class Image(models.Model):
    full_size = models.ImageField()
    thumbnail = models.ImageField()
    location = models.ForeignKey('app_label.Location', related_name='photos')

and remove the image field from the Location model.

To upload multiple photos, you’ll want to use a formset. Depending on the interface you want, you’ll probably want to use a model formset so the photos get their location_id set properly.

With your form, all you need to do is use the formset_factory function that you can call in your view (probably in get_context_data).

Handling the formset in your view involves some logic wrangling but there is a project called django-extra-views that implements the form with a formset logic, again, depending on the interface you’re going for here.

If you want to just add photos to a pre-existing location, that is much simpler: just include the model_formset with a location object.

Leave a comment