A
A
Andrey2015-05-31 10:36:12
Django
Andrey, 2015-05-31 10:36:12

How do I attach multiple photos to a post?

Hello!
There is a model, it is necessary that when creating a record, the user can attach their photos!
I see such an option (probably one of the simplest), is to add an additional QueryImage model, and use inlineformset_factory to make a form for images:

class Query(models.Model):
    # к этой модели нужно прикрепить фото
    # ...

class QueryImage(models.Model):
    query = models.ForeignKey('Query', related_name='images', verbose_name=u'Запрос')
    image = models.ImageField(u'Фото', upload_to='images/%Y/%m/%d')
    comment = models.CharField(u'Комментарии', max_length=200, blank=True)
    updated = models.DateTimeField(u'Изменен', auto_now=True)
    created = models.DateTimeField(u'Создан', auto_now_add=True)

Tell me how you implement a similar task in your projects (the task is quite popular)?
Are you using third party apps?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2015-05-31
@deliro

You have described a good one.
Instead of inline, you can throw in an API and send photos using AJAX (because, judging by the model, you do not limit the user to a specific number of photos).
By the way, use django-imagekit to compress photos (you don't know, maybe the user will want to send you a 400mb bitmap?)
And instead
use

image = ProcessedImageField(upload_to='your/path', 
                             processors=[ResizeToFit(800, 600)], 
                             format='JPEG', options={'quality': 70})

In this example, the image will fit into an 800x600 rectangle, keeping the aspect ratio. The other options are easy to guess.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question