django smart_selects,第二级不会填充

时间:2012-02-06 03:43:52

标签: jquery python django jquery-plugins

我无法将第二级(子类别/ E_cat)下拉到填充。第一级(类别)似乎工作正常。我想我已经尝试了chained_field和chained_model_field选择的所有合理组合而没有成功。

我希望Equip表填充类别的ID和子类别的ID。

以下是该模型目前的样子。我究竟做错了什么?

model.py

class Category(models.Model):
    cat_no = models.IntegerField(null=True, blank=True)
    cat_txt = models.CharField(max_length=45)    
    def __unicode__(self):
        return self.cat_txt

class E_cat(models.Model):
    cat_no = models.ForeignKey(Category)
    cat_txt = models.CharField(max_length=45)
    scat_no = models.IntegerField(null=True, blank=True)
    scat_txt = models.CharField(max_length=45)
    def __unicode__(self):
        return self.scat_txt


class Equip(models.Model):
    category = models.ForeignKey(Category)
    subcategory = ChainedForeignKey(
        E_cat,
        chained_field="category",
        chained_model_field="cat_no",
        show_all=False,
        auto_choose=True,
    )
    manufacturer = models.CharField(max_length=35, blank=True)
    mfg_no = models.CharField(max_length=35, blank=True)
    size = models.CharField(max_length=35, blank=True)
    color = models.CharField(max_length=35, blank=True)
    quanity = models.IntegerField(null=True, blank=True)
    short_description = models.CharField(max_length=80, blank=True)
    location_zip = models.IntegerField(null=True, blank=True)
    listings = models.ForeignKey(Listings)
    info = models.TextField(null=True, blank=True)

3 个答案:

答案 0 :(得分:0)

这看起来不错。问题可能是您没有设置smart_selects网址,或者您可能错过了实际网页中的javascript。

答案 1 :(得分:0)

<强> html.file

如果您使用表单

,请包含以下内容
<script src="{% static 'smart-selects/admin/js/chainedfk.js' %}"></script>
<script src="{% static 'smart-selects/admin/js/chainedm2m.js' %}"></script>

<强> models.py

class Continent(models.Model):
        name = models.CharField(max_length=255)
        def __str__(self):
            return self.name

    class Country(models.Model):
        continent= models.ForeignKey(Continent)
        name = models.CharField(max_length=255)
        def __str__(self):
            return self.name

    class City(models.Model):
        continent= models.ForeignKey(Continent)
        country= ChainedForeignKey(Country, chained_field="continent",  chained_model_field="continent", show_all=False, auto_choose=True, sort=True)
        name = models.CharField(max_length=255)
        def __str__(self):
            return self.name

    class Neighborhood(models.Model):
        continent= models.ForeignKey(Continent)
        country= ChainedForeignKey(Country, chained_field="continent",  chained_model_field="continent", show_all=False, auto_choose=True, sort=True)
        name = models.CharField(max_length=255)
        city= ChainedForeignKey(City, chained_field="country",  chained_model_field="country", show_all=False, auto_choose=True, sort=True)
name = models.CharField(max_length=255)
        def __str__(self):
            return self.name

<强> admin.py

admin.site.register(Component)
admin.site.register(Group)
admin.site.register(Failure)
admin.site.register(Neighborhood)

答案 2 :(得分:0)

更新 - 2017年5月

在jQuery版本2之后立即包含此标记。它使用python 3.5.2在Django 1.10和1.11版本中运行良好。 希望这可以帮助你

<script type="text/javascript" src="{% static 'smart-selects/admin/js/chainedfk.js' %}"></script>
<script type="text/javascript" src="{% static 'smart-selects/admin/js/chainedm2m.js' %}"></script>
<script type="text/javascript" src="{% static 'smart-selects/admin/js/bindfields.js' %}"></script>