我在使用 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 = ''