2014-09-23 70 views
0

我一直在寻找一种解决方案,我认为这是一个常见的请求,但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。

在此先感谢。

回答

0

尝试Django的聪明选择

https://github.com/PragmaticMates/django-clever-selects

我用它在我的Django 1.6的项目。在视图和表单中操作链(在巧妙的选择中),而不是在模型中(如智能选择)更有用。

+0

由于生病给一个走到今天 – Karl 2014-09-24 08:19:08

+0

嗨,好不容易才鞋垫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

+0

我有类似的东西,并寻找https://github.com/PragmaticMates/django-clever-sessions/issues/6 hack。刚才聪明的选择工作,但我不满意,期待。 – 2014-09-25 13:16:48

1

我刚刚完成了类似的项目,我想您的解决方案是:

class InstrumentEquipmentType(models.Model): 
     manufacturer_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) 
     equipment_type = models.CharField(max_length=100) 

     def __unicode__(self): # Python 3: def __str__(self): 
      return unicode(self.equipment_type) 

让我知道,如果没有工作,送你我的例子