I have a problem, when i try upload files I am trying show the user uploaded the file but not working.
My model is this:
class Document(models.Model):
id = models.AutoField(primary_key=True)
programa=models.CharField(max_length = 100)
materia=models.CharField(max_length = 50)
profesor=models.CharField(max_length = 50)
usuario=models.ForeignKey(Usuario)
add=models.DateTimeField ( auto_now = True )
archivo= models.FileField(upload_to="archivos/",storage=OverwriteStorage(),null = False)
def __unicode__(self):
return self.programa
class Meta:
db_table = u'utp_document'
My view is:
@login_required(login_url='/')
def upload(request):
if request.method=='POST':
form=DocumentForm(request.POST,request.FILES,)
if form.is_valid():
instances = form.save(commit=False)
for instance in instances:
instance.usuario = request.user
instance.save()
return HttpResponseRedirect('/menu/')
else:
form=DocumentForm()
return render_to_response('formulario_modal.html', {'form': form}, context_instance=RequestContext(request))
I followed this post Django - Auto populate created_by field outside django admin using form.
and this my form.py:
class DocumentForm(forms.ModelForm):
class Meta:
model = Document
exclude = ('usuario',)
The problem is what i try new upload file get this error:
'Document' object is not iterable
What am I doing wrong?