user3172700
user3172700

Reputation: 107

Django smart selects

I try to chain material and categorie with django smart select but it does not work

What is wrong in my code ?

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)

Upvotes: 0

Views: 1615

Answers (2)

George
George

Reputation: 169

Your structure is incorrect I am giving you an example that works

    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

Upvotes: 0

Y.N
Y.N

Reputation: 5257

Try Django Clever Selects

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

I use it in my Django 1.6 project

Upvotes: 1

Related Questions