Django Forms.py获取ModelChoiceField的主键

时间:2016-07-22 04:58:04

标签: python django django-forms

所以我一直在做很多研究,试图找出如何根据另一个字段的选定值(Drilldown)过滤字段。以下是我正在研究的一个例子。我已经看到一些看起来像django-smart-choices(django: Drill down select options?)的应用程序,但它似乎不适合我,它似乎不像我需要的那样可靠是

forms.py 

from django.forms import ModelForm
from .models import CurriculumIntervention
from Lesson.models import Curriculum
from Lesson.models import CurriculumLesson

class CurriculumInterventionEducatorsForm(ModelForm): 

    def __init__(self, *args, **kwargs):
        super(CurriculumInterventionEducatorsForm, self).__init__(*args, **kwargs)
        curriculum = self.fields['curriculum'] #This is a ModelChoiceField

        if self.instance:
            self.fields['curriculum_lesson'].queryset= \
                CurriculumLesson.objects.filter(curriculum= **____**)#<-- what do I put here to get the curriculum's primary key value?
        else: 
            self.fields['curriculum_lesson'].queryset= None

视图和模板工作正常。我无法弄清楚如何从第一个ModelChoiceField获取主键以用于第二个ModelChoiceField的查询。如果我在空白中将一个随机id值放在1中,则将所有内容过滤到该id。我想我需要知道如何获取ModelChoiceField的选定值以在查询集中使用。也许我走错了路,但那就是我现在所处的位置。我想知道是否可能必须使用视图来确定所选值并将其传递给表单(就像这篇文章似乎做Django filter the queryset of ModelChoiceField

0 个答案:

没有答案
相关问题