我一直在尋找一種解決方案,我認爲這是一個常見的請求,但Google搜索很少。我正在嘗試創建一個「級聯」下拉菜單,通常在用戶選擇國家,城市,城鎮等位置表單中找到的正常UI用戶界面功能。Django智能選擇
我一直在嘗試使用的解決方案是https://github.com/digi604/django-smart-selects。然而,這些文件很少有這樣的文件,令人感到困惑。下面是模型,因爲我有他們至今:
models.py
class InstrumentModelType(models.Model):
model_type = models.CharField(max_length=100)
def __unicode__(self): # Python 3: def __str__(self):
return unicode(self.model_type)
class InstrumentManufactuer(models.Model):
manufacturer_model_type = models.ForeignKey(InstrumentModelType)
manufacturer = models.CharField(max_length=100)
def __unicode__(self): # Python 3: def __str__(self):
return unicode(self.manufacturer)
class InstrumentEquipmentType(models.Model):
equipment_manufacturer = models.ForeignKey(InstrumentManufactuer)
equipment_type = models.CharField(max_length=100)
def __unicode__(self): # Python 3: def __str__(self):
return unicode(self.equipment_type)
class InstrumentDetails(models.Model):
instrument_model_type = models.ForeignKey(InstrumentModelType)
instrument_manufacturer = ChainedForeignKey(InstrumentManufactuer,
chained_field='instrument_model_type',
chained_model_field='manufacturer_model_type',
auto_choose = True,
show_all = False)
instrument_equipment_type = ChainedForeignKey(InstrumentEquipmentType,
chained_field='instrument_manufacturer',
chained_model_field='equipment_manufacturer',
auto_choose = True,
show_all = False)
def __unicode__(self): # Python 3: def __str__(self):
return '%s %s %s' % (self.instrument_model_type, self.instrument_manufacturer, self.instrument_equipment_type)
當我選擇從第一個下拉(instrument_model_type)的選項既不是另外兩個下拉菜單的填充預期。理想情況下,我希望能夠首先按照型號類型過濾,然後由製造商過濾以顯示可用的設備類型。任何人都可以看到我要去哪裏嗎?
我已經在文檔中描述了urls.py
中的參考文獻,並且嘗試了很多字段參考(chained_field/chained_model_field)的組合,以確保我已正確理解關係。我還注意到,widgets.py
中引用的simplejson
已折舊,因此我將其替換爲json
。
雖然在這裏拖曳帖子,我發現http://www.dajaxproject.com/forms/,但作者在Github頁面上建議不要使用該庫。
我錯在認爲這是一個共同的要求?有沒有Django的解決方案,我錯過了?如果它很重要,我使用的是Django 1.6。
在此先感謝。
由於生病給一個走到今天 – Karl 2014-09-24 08:19:08
嗨,好不容易纔鞋墊Clever-選入我的項目。除了當我提交我接收到的表單時,所有的工作都是有效的:從ajax請求返回的數據(url =/tagnumbers/ajax/chained-brand-models /,params = {'field_value':u'1','parent_value':u '1','field_name':'manufacturer'})無法反序列化爲Python對象 任何想法?一直試圖弄清楚,但迄今沒有運氣。再次感謝 – Karl 2014-09-25 13:05:02
我有類似的東西,並尋找https://github.com/PragmaticMates/django-clever-sessions/issues/6 hack。剛纔聰明的選擇工作,但我不滿意,期待。 – 2014-09-25 13:16:48