我一直在寻找一个解决方案,我认为这是一个常见的请求,但在谷歌搜索时发现很少。我试图创建一个“级联”的下拉菜单集,这是一个用户可以选择国家、城市、城镇等位置表单中常见的用户界面功能
我一直在尝试使用的解决方案是https://github.com/digi604/django-smart-selects。然而,这些文档,尽管其中很少,却相当令人困惑。以下是我目前拥有的模型:
模型.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解决方案?如果有关系的话,我使用的是django1.6。在
提前谢谢。在
试试Django聪明的选择
https://github.com/PragmaticMates/django-clever-selects
我在我的django1.6项目中使用它。在视图和表单中操作链更有用(在聪明的选择中),而不是在模型中(像smart selects那样)。在
我刚刚完成了类似的项目,我想你的解决方案是:
如果没有用我的例子告诉你
相关问题 更多 >
编程相关推荐