您可以随时尝试将FileField
分隔为FileModel
。
看看下面的伪代码(如基于内存的python - 我现在已经转移到clojure)。
models.py
class FileModel(models.Model):
file = models.FileField()
...
class ThingToWhichYoureAttaching(models.Model):
name = models.CharField()
attachments = models.ManyToManyField(FileModel)
...
forms.py
class FileForm(forms.ModelForm):
class Meta:
model=FileModel
class ThingForm(forms.ModelForm):
attachments = forms.MultipleChoiceField()#override the manytomany form field with style field of your choice.
class Meta:
model=ThingToWhichYoureAttaching
当他们弹出用加号按钮的窗口,显示FileForm
但主页上留下ThingForm
不变。对于没有javascript的人,您也可以在主页上使用ThingForm
的初始FileField。只要确保在ThingForm
之前处理FileForm
,以便文件可用于Thing。
处理弹出窗体时,您可以使用AJAX(我建议jquery)将FileForm提交给服务器,并返回标记以在附件字段中插入文件。
目前尚不清楚你的问题是什么。请你能详细解释一下。 – 2010-03-26 10:52:49
它就像gmail中的“附加另一个文件”功能,用户在其中显示文件上传字段,并且随着用户点击“附加另一个文件”加上按钮 – qulzam 2010-03-26 11:44:25
I认为你应该向我们展示更多你的代码。根据你的说法,我认为你很可能实际上试图将多个值传递给一个表单字段。你应该看一下“formset”和“inline formsets”。 – 2010-03-26 12:45:07