编辑实例时,带有filefield的Django表单会丢失文件

时间:2015-03-12 11:09:40

标签: django django-forms django-views

我试图创建一个可用于创建" LearningObjects"的新实例的表单。以及编辑现有实例。它似乎工作得很好,除了当我编辑现有实例时,我丢失了文件字段。由于这是一个必填字段,它要求我上传一个新文件,显然我并不总是想这样做。

Form.py

class LearningObjectuploadform(forms.ModelForm, edit): 
    level = forms.ModelMultipleChoiceField(widget=forms.CheckboxSelectMultiple,queryset=None,required=False)
    agebracket =forms.ModelMultipleChoiceField(widget=forms.CheckboxSelectMultiple,queryset=None,required=False)
    pathway = forms.ModelMultipleChoiceField(widget=forms.CheckboxSelectMultiple,queryset=None,required=False)
    class Meta: 
        model = LearningObject 
        fields =['title','archivefile','description','tags','pathway','level','subject','agebracket']

    def __init__(self, *args, **kwargs):
        super(LearningObjectuploadform, self).__init__(*args, **kwargs)
        self.fields['level'].queryset = AssoeLevel.objects.all()
        self.fields['pathway'].queryset = AssoePathway.objects.all()
        self.fields['agebracket'].queryset = AgeBracket.objects.all()

View.py

def createLearningobject(request,learningobject_pk=False):
if request.method == 'GET':
    if learningobject_pk:
        print learningobject_pk
        instance = LearningObject.objects.get( pk=learningobject_pk)

        print instance
        form = LearningObjectuploadform(request.POST or None, request.FILES or None, instance=instance)
    else:
        form = LearningObjectuploadform()
else:   
    form = LearningObjectuploadform(request.POST, request.FILES)
    if form.is_valid():
        learningobject = request.FILES['archivefile']
        title = form.cleaned_data['title']
        description = form.cleaned_data['description']
        tags = form.cleaned_data['tags']
        levels = form.cleaned_data['level']
        pathways = form.cleaned_data['pathway']
        agebrackets = form.cleaned_data['agebracket']
        post = LearningObject.objects.create(archivefile=learningobject,title=title, description=description)
        for tag in tags:
            post.tags.add(tag)
        for level in levels:
            post.level.add(level.pk)
        for pathway in pathways:
            post.pathway.add(pathway.pk)
        for agebracket in agebrackets:
            post.agebracket.add(agebracket.pk)
        post.save()
        return HttpResponseRedirect(reverse('index', ))
    else:
        print "form not valid"
return render(request, 'mediamanager/edit_learningobject.html', {'form': form,})

Models.py

class DefaultResource(models.Model):
#
# This class is the parent class for all resources in the media manager
#
title = models.CharField(max_length=100)
created_date = models.DateTimeField(auto_now_add=True, auto_now=False)
edited_date =  models.DateTimeField(auto_now_add=False,auto_now=True)
level = models.ManyToManyField(AssoeLevel)
agebracket= models.ManyToManyField(AgeBracket)
pathway= models.ManyToManyField(AssoePathway)
tags = TaggableManager()
slug = models.SlugField(max_length=100,editable=False,blank=True)
updownvotes = RatingField(can_change_vote=True)
views = models.DecimalField(max_digits=20,decimal_places=2,default=0,blank=True)
score = models.DecimalField(max_digits=20,decimal_places=4,default=0,blank=True)
icon = models.CharField(max_length=254,editable=False,blank=True)
subject = models.ManyToManyField(AssoeSubjects)

#def return_tags(self):
#   taglist = self.tags.names()
#   return taglist

def calculate_score(self):
    score = float(self.updownvotes.likes) - float(self.updownvotes.dislikes)
    score = score + (float(self.views)**(float(1)/float(2)))
    self.score = score
    rounded_score = int(round(self.score))
    if rounded_score < -1:
        return -1
    else:
        return rounded_score

def __unicode__ (self): 
    return self.title

def save(self, *args, **kwargs):
    self.calculate_score()
    if not self.id:
        self.slug = slugify(self.title)
    super(DefaultResource, self).save(*args, **kwargs)


class LearningObject(DefaultResource):
archivefile = models.FileField(upload_to='static/learningobject/archivefiles/%Y/%m/%d')
indexpath = models.CharField(max_length=254,editable=False,blank=True)
description = models.TextField(blank=True)
def unpackarchive(self):
    archive = self.archivefile
    filename = os.path.basename(str(archive))

    folder = str(filename).split(".")[0]
    print folder
    index_found = "False"
    with zipfile.ZipFile(archive,"r") as z:
        for each in z.namelist():
            if each == "index.html" or each == "index.htm":
                index_found = "True"
            else:
                pass
        if not index_found:
            print "zip file does not contain a valid index.html file"
        else:
            path = os.path.join("static","learningobject","unpackedarchives",folder)
            z.extractall(path)
            self.findindex(path)
def findindex(self,path):

    print path
    for root, dirnames, filenames in os.walk(path):
        for filename in fnmatch.filter(filenames, 'index.ht*'):
            print filename
            self.indexpath = os.path.join(root, filename)
    print self.indexpath

def save(self, *args, **kwargs):
    self.icon = "/static/images/icons/box.png"
    self.unpackarchive()
    super(LearningObject, self).save(*args, **kwargs)

1 个答案:

答案 0 :(得分:2)

我在django表单中遇到过类似的文件上传问题。这是我希望应该帮助你的(如果你愿意改变archivefile字段的required属性。)

if request.method == 'GET':
    if learningobject_pk:
        print learningobject_pk
        instance = LearningObject.objects.get( pk=learningobject_pk)

        print instance
        form = LearningObjectuploadform(request.POST or None, request.FILES or None, instance=instance)
        form.base_fields['archivefile'].required = False

    else:
        form = LearningObjectuploadform()