如何在Django Admin后台添加一个省市二级联动下拉列表
1个回答
2015-03-01 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517193
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
得确是可以,不过过程有点多, 以下代码仅供参考,请自行调整
1. 定义数据来源, 这里简单的定义一下,如 settings.py
LOCATION = {"浙江": ["杭州", "宁波"], "湖北": ["武汉"]}
2. models.py
COUNTRY = [(key, key) for key in sorted(settings.LOCATION.keys())]
class Company(models.Model):
country = models.CharField(max_length=50, null=True, blank=True, verbose_name=_("Country"), choices=COUNTRY)
state = models.CharField(max_length=50, null=True, blank=True, verbose_name=_('State'))
3. admin.py
class CompanyAdminForm(forms.ModelForm):
state = MyChoiceField(label=_('State'), required=False)
def locations(self):
import json
return json.dumps(settings.LOCATION)
def __init__(self, *args, **kwargs):
ins = kwargs.get('instance')
super(CompanyAdminForm, self).__init__(*args, **kwargs)
state = self.fields['state']
if ins and ins.country:
state.choices = [(item, item) for item in settings.LOCATION.get(ins.country)]
class Meta:
model = Company
class CompanyAdmin(admin.ModelAdmin):
form = CompanyAdminForm
4. templates/admin/app/company/change_form.html
复制原change_form.html内容 在合适位置加入(自行写js代码 )
<script type="text/javascript">
var LOCATION = {{ adminform.form.locations | safe }};
var $ = django.jQuery;
$(document).ready(function(){
$('#id_country').change(function(){
var country =$(this).val();
$('#id_state').empty();
if (country ){
var state = LOCATION[country];
var i;
for (i=0; i < state.length; i++){
var v = state[i];
$('#id_state').append("<option value='" + v + "'>" + v + "</option>");
}
} else {
$('#id_state').append("<option value=''>" + "---------" + "</option>");
}
});
});
</script>
1. 定义数据来源, 这里简单的定义一下,如 settings.py
LOCATION = {"浙江": ["杭州", "宁波"], "湖北": ["武汉"]}
2. models.py
COUNTRY = [(key, key) for key in sorted(settings.LOCATION.keys())]
class Company(models.Model):
country = models.CharField(max_length=50, null=True, blank=True, verbose_name=_("Country"), choices=COUNTRY)
state = models.CharField(max_length=50, null=True, blank=True, verbose_name=_('State'))
3. admin.py
class CompanyAdminForm(forms.ModelForm):
state = MyChoiceField(label=_('State'), required=False)
def locations(self):
import json
return json.dumps(settings.LOCATION)
def __init__(self, *args, **kwargs):
ins = kwargs.get('instance')
super(CompanyAdminForm, self).__init__(*args, **kwargs)
state = self.fields['state']
if ins and ins.country:
state.choices = [(item, item) for item in settings.LOCATION.get(ins.country)]
class Meta:
model = Company
class CompanyAdmin(admin.ModelAdmin):
form = CompanyAdminForm
4. templates/admin/app/company/change_form.html
复制原change_form.html内容 在合适位置加入(自行写js代码 )
<script type="text/javascript">
var LOCATION = {{ adminform.form.locations | safe }};
var $ = django.jQuery;
$(document).ready(function(){
$('#id_country').change(function(){
var country =$(this).val();
$('#id_state').empty();
if (country ){
var state = LOCATION[country];
var i;
for (i=0; i < state.length; i++){
var v = state[i];
$('#id_state').append("<option value='" + v + "'>" + v + "</option>");
}
} else {
$('#id_state').append("<option value=''>" + "---------" + "</option>");
}
});
});
</script>
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询