Django:如何动态过滤下拉框?

时间:2014-10-29 15:48:09

标签: python django

比如说我有三个型号。 ContentChapterPage。在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

1 个答案:

答案 0 :(得分:1)

我建议你两个选择:

  1. 使用django-autocomplete-light
  2. 使用像this
  3. 这样的jquery