Django聪明选择

时间:2014-03-04 07:45:37

标签: django chain

我尝试使用django smart select链接材料和类别,但它不起作用

我的代码有什么问题?

class Demande_Expertise(models.Model):
    user = models.ForeignKey(User)
    material = models.ForeignKey("Material")
    categorie =  ChainedForeignKey("material.Category",
                          chained_field="material",
                          chained_model_field="name",
                          show_all=False,
                          auto_choose=True)                         
    droits_acces = models.CharField(_('val_champ'), max_length=150, choices = DROITS)
    groupe = models.ForeignKey(Group, blank = True, null= True, default = None)
    etat = models.CharField(_('val_champ'), max_length=150, choices = ETAT, default = '2')

class Category(models.Model):
    name = models.CharField(_('name'), max_length=50)
    slug = models.SlugField()

class Material(models.Model):
    name = models.CharField(_('name'), max_length=50)
    description = models.TextField(_('description'), blank=True)
    slug = models.SlugField()
    category = ChainedForeignKey(Category, verbose_name=_('category'),
                          chained_field="name",
                          chained_model_field="name",
                          show_all=False,
                          auto_choose=True)
    created = models.DateField(_("creation date"), auto_now_add=True)

2 个答案:

答案 0 :(得分:1)

尝试Django Clever选择

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

我在我的Django 1.6项目中使用它

答案 1 :(得分:0)

你的结构不正确我给你一个有效的例子

    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