need help on multiple file uploads

20 views
Skip to first unread message

Robb Rodirguez Jr.

unread,
Mar 12, 2020, 11:35:13 PM3/12/20
to Django users
im having to with multiple file uploads, when i tried to upload files only 1 file will be saved..

here's my code:
views.py
def attachments(request):
to = TravelOrder.objects.order_by('-date_filling').last()
if request.method == 'POST':
form = AttachmentsForm(request.POST, request.FILES)
if form.is_valid():
for f in request.FILES.getlist('attachment'):
instance = form.save(commit=False)
instance.travel_order = to
instance.attachment = f
instance.save()
print('YEW')
return redirect('attach')

else:
form = AttachmentsForm()

context = {
'form': form
}
return render(request, 'employee/attachments.html', context)
models.py
class TravelOrder(models.Model):
created_by = models.CharField(max_length=255)
start_date = models.DateField(auto_now=False)
end_date = models.DateField(auto_now=False)
wfp = models.CharField(max_length=255, verbose_name='Wfp Where to be charged')
purpose_of_travel = models.CharField(max_length=255)
region = models.ForeignKey(Region, on_delete=models.CASCADE)
venue = models.CharField(max_length=255)
date_filling = models.DateTimeField(auto_now_add=True)
status = models.CharField(max_length=15)

def __str__(self):
return self.purpose_of_travel

class Attachements(models.Model):
at_id = models.AutoField(primary_key=True)
travel_order = models.ForeignKey(TravelOrder, on_delete=models.CASCADE)
attachment = models.FileField(upload_to='attachment/')

forms.py
class AttachmentsForm(forms.ModelForm):
class Meta:
model = Attachements
exclude = ['travel_order']
fields = ('attachment',)
widgets = {
'attachment': forms.ClearableFileInput(attrs={'multiple':True})
}


hope you can help me guys. thank you..
Reply all
Reply to author
Forward
0 new messages