由于您是在URL中存储学校的子d,因此最好不要在表格中添加“学校”字段。然后,您可以使用以下
form_valid方法设置学校:
from django.shortcuts import get_object_or_404class StudentCreateView(CreateView): fields = ("name","age",) # don't include 'school' here ... def form_valid(self, form): school = get_object_or_404(School, slug=self.kwargs['school']) form.instance.school = school return super(StudentCreateView, self).form_valid(form)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)