在文件上传时获取'builtin_function_or_method对象是不可迭代的'

时间:2012-08-22 12:57:25

标签: django file-upload

我无法上传文件。我正在

Type error builtin_function_or_method' object is not iterable

models.py

class seeker(models.Model):
    user = models.OneToOneField(User)
    birthday = models.DateField()

class Upload(models.Model):
    user = models.ForeignKey(Seekers)
    resume = models.FileField(upload_to ='resume', blank = True, null = True)

forms.py

class SeekersForm(forms.Form):
    resume = forms.FileField(label = 'Select a file',help_text = 'max.3 MB')

views.py

def List(request):

    # Handle file upload
    if request.method == 'POST':
        form = SeekersForm(request.POST, request.FILES)
        if form.is_valid():

            #id = User.object.get(id)
            newdoc = Seekers.objects.get(user_id)
            newdoc.resume =Upload(resume = request.FILES['resume'])
            newdoc.save()

            #seekers_edit = Seekers.objects.get(id) 
            #seekers_edit.resume = Seekers(resume = request.FILES['resume'])
            #seekers_edit.save()

            #Redirect to the document list after POST
            return HttpResponseRedirect('/profile/')
    else:
        form = SeekersForm() # A empty, unbound form

    #Load documents for the list page   
    seekers = Seekers.objects.all()

    #Render list page with the documents and the form
    return render_to_response('list.html',{'seekers':seekers,'form':form},context_instance=RequestContext(request))

2 个答案:

答案 0 :(得分:0)

很难说你的问题在哪里,但我认为以下代码行是主要问题:

newdoc.resume =Upload(resume = request.FILES['resume'])

在保存整个模型实例之前,必须显式地将文件保存在FileField中。此外,如果您的某个模型中有ForeignKey字段,并且您想为其分配另一个模型的实例,请在执行分配之前先保存该实例。在不了解您的Seekers模型的情况下,我所能做的就是猜测可能对您有什么帮助。以下内容可能会让您入门:

your_file = request.FILES['resume']
upload_instance = Upload()
upload_instance.resume.save(name=your_file.name, content=your_file, save=False)
upload_instance.user = ... # Here goes an instance of your Seekers model
upload_instance.save() # Here you save the whole instance of your Upload model

另外,请注意以下事项:

  1. 您的模型Seekers应该使用单数而非复数命名为Seeker。这应该与所有模型一样。
  2. Python函数应始终以小写字母开头,即list而不是List。但是,这个名称无论如何都是一个糟糕的选择,因为Python的标准库中已经存在一个名为list的函数。
  3. 请仔细看看Django的文档。这一切都在你需要知道的地方。我建议你特别阅读以下部分:

    https://docs.djangoproject.com/en/1.4/ref/models/fields/#filefield

    https://docs.djangoproject.com/en/1.4/ref/files/file/

答案 1 :(得分:0)

代码中的问题:

  • 您的表单定义会复制模型中的信息 - 只需使用forms.ModelForm(使用exclude,以便不显示user字段)
  • 目前已粘贴,newdoc = Seekers.objects.get(user_id)会引发TypeError'foo' object is not iterable); .get() accepts keyword parameter filters,而不是其他任何内容。
  • 无需手动访问request.FILES['resume']

所以,简而言之,你几乎就在那里;让Django表单为您完成更多工作:

# forms.py
class SeekerForm(forms.ModelForm)
    class Meta:
        model = Seeker

# views.py    
def seeker_list(request):
    # Opinions are divided as to whether it's ever appropriate to 
    # modify the database like this on a GET request, but it seems
    # to make sense here
    seeker = Seekers.objects.get_or_create(user=request.user)
    if request.method == 'POST':
        form = SeekerForm(request.POST, request.FILES, instance=seeker)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/profile/')
    else:
        form = SeekerForm(instance=seeker)

    seekers = Seekers.objects.all()

    #Render list page with the documents and the form
    return render_to_response('list.html', {
        'seekers':seekers,
        'form':form
    }, context_instance=RequestContext(request))

目前尚不清楚代码中已注释部分的重要性(如果有的话) - 我假设您总是想要修改当前用户的Seeker,但如果没有,则根据需要进行调整。