Reputation: 951
I have a House and a HouseImage models:
class House(models.Model):
...
class HouseImage(models.Model):
image = models.ImageField()
house = models.ForeignKey(House, related_name='images')
The problem is to allow users in admin page upload multiple HouseImages. Now I have HouseImageAdmin as an inline of HouseAdmin and in this case user can't add multiple HouseImages by uploading multiple files, user must add new inline object then upload file then repeat those actions N times. I want to add one multiple file upload widget which will automatically add HouseImage for each uploaded file. How can I do it?
Upvotes: 1
Views: 1175
Reputation: 951
I did it. Just made a custom admin form:
class HouseAdminForm(forms.ModelForm):
images = forms.FileField(
widget=forms.ClearableFileInput(attrs={'multiple': True}),
label=u'Фото дома',
required=False,
)
class Meta:
model = House
fields = '__all__'
def save(self, *args, **kwargs):
house = super(HouseAdminForm, self).save(*args, **kwargs)
if hasattr(self.files, 'getlist'):
for f in self.files.getlist('images'):
HouseImage.objects.create(house=house, image=f)
return house
Upvotes: 1