Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

customizing admin of django to have dependent select fields

I am new in django , I am developing admin panel first for my site. I need to have dependent select fields, so that after selecting country, user will be able to select city. I want that when I select country then cities of that country options load in city select box but I don't know how to customize django admin. I have done so in JS, using AJAX and PHP. So I know how to do it manually but don't know how to use ajax in django and how to customize it.

On some other questions I read that one should read, django documentation, so I tried to read and find at admin documentation but couldn't found way to customize django admin panel. Do I need to customize full page for those select boxes? Or please tell that how can I add some jquery code to it so that I can do it via JS?

like image 983
Hafiz Avatar asked Dec 05 '11 01:12

Hafiz


1 Answers

You probably want to use the feature Grouped Selects in django smart selects. From the README:

If you have the following model:

class Location(models.Model)
    continent = models.ForeignKey(Continent)
    country = models.ForeignKey(Country)

And you want that all countries are grouped by the Continent and that Groups are used in the select change to the following:

from smart_selects.db_fields import GroupedForeignKey

class Location(models.Model)
    continent = models.ForeignKey(Continent)
    country = GroupedForeignKey(Country, "continent")
like image 102
dan-klasson Avatar answered Nov 03 '22 01:11

dan-klasson