B
B
blackbb2016-09-26 15:31:42
Django
blackbb, 2016-09-26 15:31:42

How in django in the form of editing the field filefield to display the files already there?

Hello. The essence of the problem:
1. There is a Company model:

class Company(models.Model):
    title = models.CharField(max_length=100, verbose_name=u'Название компании', blank=True)
    ...

2. There is a CompanyImage model:
class CompanyImage(models.Model):
    file = models.FileField(upload_to='images/company',blank=True, null=True,verbose_name=u'Фотографии')
    order = models.ForeignKey(Company, verbose_name=u'Фото компании')

There is a form for creating a company:
class AddCompanyForm(forms.ModelForm):
    files = MultiFileField(min_num=1, max_num=7, max_file_size=1024*1024*5, label=CompanyImage._meta.get_field_by_name('file')[0].verbose_name)
    class Meta:
        model = Company
    def save(self, commit=True):
        instance = super(AddCompanyForm, self).save(commit)
        for each in self.cleaned_data['files']:
            CompanyImage.objects.create(file=each, order=instance)
        return instance

It is also used for editing. So the problem is when editing the files field is empty and requires adding a photo. How to make the field display the files already available during creation?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zelsky, 2016-09-26
@zelsky

Update view in generic views

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question