Skip to content

Instantly share code, notes, and snippets.

@maxpoletaev
Last active December 25, 2019 12:36
Show Gist options
  • Save maxpoletaev/4f188374a0daba54302fd406a58a590e to your computer and use it in GitHub Desktop.
Save maxpoletaev/4f188374a0daba54302fd406a58a590e to your computer and use it in GitHub Desktop.
class AlbumAdminForm(forms.ModelForm):
photos_batch = forms.FileField(
widget=forms.FileInput(attrs={
'accept': 'image/png, image/jpeg',
'multiple': True,
}),
required=False,
label='Photos',
help_text='Choose multiple files.',
)
class Meta:
model = Album
fields = '__all__'
@admin.register(Album)
class AlbumAdmin(admin.ModelAdmin):
form = AlbumAdminForm
def save_related(self, request, form, formsets, change):
super().save_related(request, form, formsets, change)
photos_batch = request.FILES.getlist('photos_batch')
for photo in photos_batch:
form.instance.photos.create(image=photo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment