的管理员以具有依赖的选择字段

的管理员以具有依赖的选择字段

本文介绍了自定义 django 的管理员以具有依赖的选择字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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?

解决方案

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")

这篇关于自定义 django 的管理员以具有依赖的选择字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 16:26