Django管理员内联中的“国家州城市”下拉菜单

Django管理员内联中的“国家州城市”下拉菜单,第1张

Django管理员内联中的“国家/州/城市”下拉菜单

有了一点黑客,这是相当可行的。

在以下示例中,使用County代替State和Municipality代替City。因此,模型如下:

class County(models.Model):    name = models.CharField(_('Name'), max_length=100, unique=True)class Municipality(models.Model):    county = models.ForeignKey(County, verbose_name=_('County'))    name = models.CharField(_('Name'), max_length=100)class Location(models.Model):    name = models.CharField(max_length=100)    county = models.ForeignKey(County, verbose_name=_('County'))    municipality = models.ForeignKey(Municipality, verbose_name=_("Municipality"))

问题有两个方面:客户端Javascript和服务器端字段呈现。

客户端Javascript(使用JQuery,假定从/site_media/js/municipality.js提供)如下:

var response_cache = {};function fill_municipalities(county_id) {  if (response_cache[county_id]) {    $("#id_municipality").html(response_cache[county_id]);  } else {    $.getJSON("/municipalities_for_county/", {county_id: county_id},      function(ret, textStatus) {        var options = '<option value="" selected="selected">---------</option>';        for (var i in ret) {          options += '<option value="' + ret[i].id + '">' + ret[i].name + '</option>';        }        response_cache[county_id] = options;        $("#id_municipality").html(options);      });  }}$(document).ready(function() {  $("#id_county").change(function() { fill_municipalities($(this).val()); });});

现在,您需要Ajax视图来服务属于给定县的市政当局(假定从/ municipalities_for_county /服务):

from django.http import JSonResponsefrom django.utils.encoding import smart_uniprefrom django.utils import simplejsonfrom myproject.places.models import Municipalitydef municipalities_for_county(request):    if request.is_ajax() and request.GET and 'county_id' in request.GET:        objs = Municipality.objects.filter(county=request.GET['county_id'])        return JSonResponse([{'id': o.id, 'name': smart_unipre(o)} for o in objs])    else:        return JSonResponse({'error': 'Not Ajax or no GET'})

最后,admin.py中用于呈现该字段的服务器端代码如下。一,进口:

from django import formsfrom django.forms import widgetsfrom django.forms.util import flatattfrom django.utils.encoding import smart_uniprefrom django.utils.safestring import mark_safefrom django.contrib import adminfrom django.utils.translation import ugettext_lazyfrom myproject.places.models import Municipality, Location

然后,小部件:

class MunicipalityChoiceWidget(widgets.Select):    def render(self, name, value, attrs=None, choices=()):        self.choices = [(u"", u"---------")]        if value is None: # if no municipality has been previously selected, # render either an empty list or, if a county has # been selected, render its municipalities value = '' model_obj = self.form_instance.instance if model_obj and model_obj.county:     for m in model_obj.county.municipality_set.all():         self.choices.append((m.id, smart_unipre(m)))        else: # if a municipality X has been selected, # render only these municipalities, that belong # to X's county obj = Municipality.objects.get(id=value) for m in Municipality.objects.filter(county=obj.county):     self.choices.append((m.id, smart_unipre(m)))        # copy-paste from widgets.Select.render        final_attrs = self.build_attrs(attrs, name=name)        output = [u'<select%s>' % flatatt(final_attrs)]        options = self.render_options(choices, [value])        if options: output.append(options)        output.append('</select>')        return mark_safe(u'n'.join(output))

接下来,形式:

class LocationForm(forms.ModelForm):    municipality = forms.ModelChoiceField(Municipality.objects, widget=MunicipalityChoiceWidget(), label=ugettext_lazy("Municipality"), required=False)    class meta:        model = Location    def __init__(self, *args, **kwargs):        """        We need access to the county field in the municipality widget, so we        have to associate the form instance with the widget.        """        super(LocationForm, self).__init__(*args, **kwargs)        self.fields['municipality'].widget.form_instance = self

最后是admin类:

class LocationAdmin(admin.ModelAdmin):    form = LocationForm    class Media:        js = ('http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js',     '/site_media/js/municipality.js')admin.site.register(Location, LocationAdmin)

让我知道是否还有不清楚的地方。



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5017665.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存