2

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?

Community
  • 1
  • 1
papo
  • 155
  • 1
  • 6

1 Answers1

0

form.save() returns a single modal instance:

instance = form.save(commit=False)
instance.usuario.user = request.user
instance.save()

FYI, in the post you've linked a formset is used which is the reason for a loop.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • but I get the error with which I have been trying to solve this is "Cannot assign ">": "Document.usuario" must be a "Usuario" instance." – papo Apr 02 '15 at 23:42
  • @papo this is better and a different problem. Can you show how `Usuario` modal is defined? Thanks. – alecxe Apr 02 '15 at 23:43
  • class UsuarioForm(forms.ModelForm): class Meta: model = Usuario fields = ['nombres','correo'] equals the DocumentForm – papo Apr 02 '15 at 23:49
  • @papo nono, I'm asking about the `Usuario` model. – alecxe Apr 03 '15 at 00:03
  • ahhh is this class Usuario(models.Model): user = models.ForeignKey(User) nombres = models.CharField(max_length = 50) correo = models.EmailField() class Meta: db_table = u'utp_users' – papo Apr 03 '15 at 17:35
  • @papo please check the update. Hope it works for you. – alecxe Apr 03 '15 at 17:37
  • Thanks for your patience but the error that appears is similar to the previous and this Document has no usuario is a bug the django 1.7.6?? – papo Apr 03 '15 at 17:52
  • the only solution I could find to this dilemma was to change the user variable in the model change Document usuario = models.ForeignKey (User) for usuario = models.CharField ( max_length = 100 ) and apply the changes you suggested and it works. shame because I wanted to do with foreingkey but good intention that counts thanks :D – papo Apr 03 '15 at 18:55