我收到此错误“NOT NULL constraint failed: myfiles_document.user_id”我试图做的是将文件附加到用户 ForeignKey 所以用户只能看到他们使用这个应用程序django-file-form上传的内容这里是项目的代码
模型.py
class Example2(models.Model):
title = models.CharField(max_length=255)
class ExampleFile(models.Model):
fs = FileSystemStorage(location=settings.MEDIA_ROOT)
input_file = models.FileField(max_length=255, upload_to='uploads/%Y.%m.%d' , storage=fs)
user = models.ForeignKey('auth.User')
def get_upload_path(self,filename):
return "static/uploads/"+str(self.user.id)+"/"+filename
表格.py
class BaseForm(FileFormMixin, django_bootstrap3_form.BootstrapForm):
title = django_bootstrap3_form.CharField()
class MultipleFileExampleForm(BaseForm):
input_file = MultipleUploadedFileField()
def save(self):
example = Example2.objects.create(
title=self.cleaned_data['title']
)
for f in self.cleaned_data['input_file']:
ExampleFile.objects.create(
input_file=f
)
self.delete_temporary_files()
视图.py
class BaseFormView(generic.FormView):
template_name = 'example_form.html'
def get_success_url(self):
return reverse('example_success')
def form_valid(self, form):
form.save()
return super(BaseFormView, self).form_valid(form)
class ExampleSuccessView(generic.TemplateView):
template_name = 'success.html'
class MultipleExampleView(LoginRequiredMixin, BaseFormView):
form_class = forms.MultipleFileExampleForm