Search code examples
djangodjango-modelsdjango-admindjango-smart-selects

Django Smart Selects not working for django admin


So I'm trying to implement django-smart-selects 1.5.9 for django 1.11.4 and somehow the chained keys dont work the way they should. models.py

class Province(models.Model):
    name = models.CharField(
        max_length=30
    )
    class Meta:
        verbose_name = _("province")
        verbose_name_plural = _("provinces")

    def __str__(self):
        return self.name

class District(models.Model):
    province = models.ForeignKey(
        Province, 
        on_delete=models.CASCADE
    )
    name = models.CharField(
        max_length=50
    )
    class Meta:
        verbose_name = _("district")
        verbose_name_plural = _("districts")

    def __str__(self):
        return self.name

class City(models.Model):
    district  = models.ForeignKey(
        District, 
        on_delete=models.CASCADE
    )
    name = models.CharField(
        max_length=50
    )
    class Meta:
        verbose_name = _("city")
        verbose_name_plural = _("cities")

    def __str__(self):
        return self.name

class Ward(models.Model):
    city = models.ForeignKey(
        City,
        on_delete=models.CASCADE
    )
    name = models.CharField(
        max_length=2
    )
    class Meta:
        verbose_name = _("ward")
        verbose_name_plural = _("wards")

    def __str__(self):
        return self.name
class School(models.Model):
   # other fields .....
    province = models.ForeignKey(
        Province, 
        on_delete=models.SET_NULL, 
        blank=True,
        null = True,
        verbose_name = _("province")
    )
    district = ChainedForeignKey(
        District,
        chained_field="province",
        chained_model_field="province",
        show_all=False,
    )
    city = ChainedForeignKey(
        City,
        chained_field="district",
        chained_model_field="district",
        show_all=False,
    )
    ward = ChainedForeignKey(
        Ward,
        chained_field="city",
        chained_model_field="city",
        show_all=False,
    )

urls.py

    url(r'^admin/', admin.site.urls),
    url(r'^admin/', include('smart_selects.urls')),

admin.py

@admin.register(School)
class SchoolAdmin(admin.ModelAdmin):
    inlines = [ServerInline, ServerUpdateInline]
    list_display = ['school_name', 'school_type','phone', 
    'province', 'district', 'city', 'ward',
]
    search_fields = ('school_name','district__name')
    list_filter = ('school_type', 'district')

here tried the chained dropdown implementation from django-smart-selects the models and admin are on a seprate app called school and the url i provided is on the base django directory's url.py

enter image description here


Solution

  • Your current url is

    url(r'^admin/', admin.site.urls),
    url(r'^admin/', include('smart_selects.urls')),
    

    I checked the docs and they use chaining instead of admin/. Could you try following their docs and see if that works?

    url(r'^admin/', include(admin.site.urls)),
    url(r'^chaining/', include('smart_selects.urls')),