比如说我有三个型号。 Content
,Chapter
和Page
。在Content
形式内,将有两个下拉框。一个用于chapters
,另一个用于pages
。如果我要从下拉框中选择chapter
,我该如何过滤page
下拉框仅显示pages
内的chapter
。
models.py
class Page(Models.Model):
# Some details about the page
class Chapter(models.Model):
# Some detail about the chapter
class Content(models.Model):
chapter = models.ForeignKey(Chapter)
page = models.ForeignKey(Chapter)
views.py
def create_contents(request):
if request.POST:
form = ContentForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return HttpResponseRedirect('/books/all/')
else:
form = ContenttForm()
args = {}
args.update(csrf(request))
args['form'] = form
return render_to_response('content/content.html', args)
forms.py
class ContentForm(forms.ModelForm):
class Meta:
model = Content