2

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

Anee Mes
  • 27
  • 9

1 Answers1

1

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')),
yujinyuz
  • 84
  • 1
  • 5
  • tried that as well, it doesn't work – Anee Mes Mar 23 '23 at 05:59
  • Were there any errors in Django or in the browser console? Could you post them here if there are any? – yujinyuz Mar 24 '23 at 01:35
  • no, there's no error in console or terminal. It only lists the provinces but the other dropdowns are empty and i've added a screenshot of the dropdown's apperances. – Anee Mes Mar 24 '23 at 03:51
  • did you set `JQUERY_URL = True` in your settings.py? Also, could you try to remove or comment out `city` and `ward` just so the `School` model only contains `district` chained foreign key and see if that works. I saw a github issue that might be related to your case: https://github.com/jazzband/django-smart-selects/issues/316 And it seems that the package doesn't support multiple chained foreign keys in one model – yujinyuz Mar 24 '23 at 04:53
  • I had `USE_DJANGO_JQUERY = True` in settings.py and changing it to `JQUERY_URL=True` raises the error `AttributeError at /en/admin/school/schoolprofile/add/ 'bool' object has no attribute 'startswith' Request Method: GET Request URL: http://127.0.0.1:8000/en/admin/school/schoolprofile/add/ Django Version: 1.11.4 Exception Type: AttributeError Exception Value: 'bool' object has no attribute 'startswith' Exception Location: C:\chained_admin\env\lib\site-packages\django\forms\widgets.py in absolute_path, line 90` – Anee Mes Mar 24 '23 at 05:47
  • Alright. I don't have much idea on what's going on there but yeah, your settings.py looks correct. Have you tried my other suggestion? > Also, could you try to remove or comment out city and ward just so the School model only contains district chained foreign key and see if that works. I saw a github issue that might be related to your case: github.com/jazzband/django-smart-selects/issues/316 And it seems that the package doesn't support multiple chained foreign keys in one model – yujinyuz Mar 24 '23 at 06:32
  • Yeah, its that the package doesnt support multiple chainedforeignkey in one model. Do you have any idea how i can implement the functionality in django admin. I did it in client side with some modelform modification and using ajax to load required districts related to province and so on – Anee Mes Mar 24 '23 at 11:21
  • i think it would be the same concept within django admin where you have to perform some modifications, check the exposed endpoints of the districts and related province, add listeners to the dropdowns – yujinyuz Mar 25 '23 at 08:01