使用Django和Python的下拉依赖

时间:2013-05-15 09:37:48

标签: django drop-down-menu dependencies

我仍然是使用django和python的新手。我正在研究HWname和HWType中的下拉依赖项。我尝试了很多代码,但它们没有用。这是我的代码。请帮帮我:)。

我想要发生的是例如在HWType中我有Flexi和Flexi2,然后在HWName中我有女性和男性..如果我在HWType中选择Flexi,只有“女性”将显示在我的HWName中并且如果我选择Flexi2,HWName将显示“男性”..

Models.Py(代码)

class HWName(models.Model):
    hwnid = models.AutoField(max_length=6, primary_key=True, db_column='id')
    hwnname = models.CharField(max_length=255, db_column='name')

    class Meta:
        db_table=u'hw_name'
    def __unicode__(self):
        return unicode((self.hwnname))


class HWType(models.Model):
    hwtid = models.AutoField(max_length=6, primary_key=True, db_column='id')
    hwtname = models.CharField(max_length=255, db_column='name')

    class Meta:
        db_table=u'hw_type'
    def __unicode__(self):
        return unicode((self.hwtname))

SCBT库存(预先搜索)

class Inventory (models.Model):
    id = models.AutoField(max_length=6, primary_key=True, db_column='id')
    hwname = models.ForeignKey(HWName, db_column='hw_name', default=1)
    hwtype = models.ForeignKey(HWType, db_column='hw_type', default=1)

    class Meta:
        db_table=u'inventory'
    def __unicode__ (self):
        return unicode((self.assetnumber, self.serialnumber, self.hwname, self.hwtype))

高级搜索硬件表单

class AdvancedSearchForm (ModelForm):
    hwname = forms.ModelChoiceField(queryset=HWName.objects.all(), label="Hardware Name")
    hwtype = forms.ModelChoiceField(queryset=HWType.objects.all(), label="Hardware Type")

    class Meta:
        model = Inventory
        exclude = ('id', 'assetnumber', 'serialnumber', 'remarks')

Views.Py(代码)

def inventory (request):

    ##advanced search form:##
    form = AdvancedSearchForm(request.POST or None)

        ##check if form is not empty##  
    hwname = request.GET.get('hwname', 0)
    hwtype = request.GET.get('hwtype',0)

    if hwname == 'None':
        hwname = ''
    if hwtype == 'None':
        hwtype = ''

1 个答案:

答案 0 :(得分:0)

尝试“django-smart-choices”
看起来像你需要的东西 https://github.com/digi604/django-smart-selects