我正在Django中编写一个应用程序,它使用DB中的文件路径引用将文件上传到文件系统。
我正在使用一个子对象来处理输入文件并将其存储在文件系统中。我在Django中使用formset也是如此。
这里的问题是我遇到了ValidationError
def trainmodule(request):
class RequiredFormSet(BaseFormSet):
def __init__(self, *args, **kwargs):
super(RequiredFormSet, self).__init__(*args, **kwargs)
for form in self.forms:
form.empty_permitted = False
TrainingContentFormSet = formset_factory(TrainingContentForm, max_num=10, formset=RequiredFormSet)
if request.method == "POST":
tmform = TrainingModuleForm(request.POST)
tcformset = TrainingContentFormSet(request.POST, request.FILES)
module_save = tmform.save()
for tc in tcformset:
content_save = tc.save(commit=False)
content_save = ModelWithFileField(file_field = request.FILES['file'])
content_save.tmodule = module_save
content_save.save()
return HttpResponseRedirect('hello')
else:
tmform = TrainingModuleForm()
tcformset = TrainingContentFormSet()
return render(request, 'training-module.html', {'tmform': tmform, 'tcformset': tcformset})
class TrainingModule(models.Model):
name = models.CharField(max_length=30)
description = models.TextField()
final_url = models.URLField()
def __unicode__(self):
return self.name
class TrainingContent(models.Model):
inputfile = models.FileField(upload_to="input_documents/")
outputfile = models.FileField(upload_to='converted/')
name = models.CharField(max_length=50)
filehash = models.CharField(max_length=255)
desc = models.TextField()
tmodule = models.ForeignKey(TrainingModule)
def __unicode__(self):
return self.name
from security_awareness.models import TrainingModule, TrainingContent
from django.forms import ModelForm
class TrainingModuleForm(ModelForm):
class Meta:
model = TrainingModule
fields = ['name', 'description']
class TrainingContentForm(ModelForm):
class Meta:
model = TrainingContent
exclude = ('tmodule', )
fields = ['name', 'desc', 'inputfile']
我对Python和Django都比较陌生。
任何帮助将不胜感激。感谢。