我有一个简单的Student模型,我希望能够为每个学生存储未知数量的文档。 我想过为模型添加一个FileField ,但是我必须为每个文档添加一个FileField 。
我的问题是为每个学生存储文档的最有效方式是什么?
编辑:
我想过使用OneToMany字段的另一个模型,但后来因为我还需要在他的管理更改表单中显示与特定Student相关的所有文件。
I have a simple model of a Student and I'd like to be able to store an unknown amount of documents for each student. I thought about adding a FileField to the model but then I'd have to add a FileField for each document.
My question is what is the most efficient way to store documents for each student?
Edit:
I thought about using another model with a OneToMany field but then I got stuck because I also need to display all the associated files with a specific Student in his admin change form.
最满意答案
正如Mounir在评论中提到的那样,我必须创建另一个模型(让我们称之为“ Document ”)来将包含FileField和ForeignKey的文件包含到Student模型中。
之后我不得不将该Document模型分配给InlineModelAdmin (我使用了TabularInline ):
class DocumentInline(admin.TabularInline): model = Document然后只需通过内inlines将其添加到主ModelAdmin :
class StudentAdmin(admin.ModelAdmin): ... inlines = [ DocumentInline, ]As Mounir mentioned in the comments, I had to make another model (let's call it "Document") to contain the file with FileField and a ForeignKey to the Student model.
After that I had to assign that Document model to a InlineModelAdmin (I used TabularInline):
class DocumentInline(admin.TabularInline): model = DocumentAnd then just add it to the main ModelAdmin through inlines:
class StudentAdmin(admin.ModelAdmin): ... inlines = [ DocumentInline, ]更多推荐
发布评论