Django管理员显示分层下拉式筛选器 [英] Django admin. Displaying a hierarchical dropdown filter

查看:181
本文介绍了Django管理员显示分层下拉式筛选器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下模型:

from django.db import models

class State(models.Model):
    name = models.CharField(max_length=30)
    abbreviation = models.CharField(max_length=2)

    def numberOfCities(self):
        return self.city_set.count()

    def __unicode__(self):
        return u"{0} - {1}".format(self.abbreviation, self.name)

class City(models.Model):
    name = models.CharField(max_length=40)
    state = models.ForeignKey(State)

    class Meta:
        verbose_name_plural = 'Cities'

    def __unicode__(self):
        return self.name

class Company(models.Model):
    name = models.CharField(max_length=60)
    description = models.CharField(max_length=1000)
    city = models.ForeignKey(City)

    class Meta:
        verbose_name_plural = 'Companies'

    def __unicode__(self):
        return self.name;

您可以看到每个公司都与一个城市相关联,正如您所期望的,Django管理员会生成包含城市下拉菜单的公司创作表。但是为了提高用户体验,我希望用户首先选择状态,然后城市下拉列表将填充该州的城市。有没有一个标准的方法?

As you can see each company is associated with a city and as you would expect the Django admin generates the company creation form containing a dropdown of cities. But in order to improve the user experience I would like the user to first select the state and then the city dropdown would be populated with cities from that state. Is there a standard way of doing this?

推荐答案

你需要创建一个自定义的小部件来选择城市模型(也就是说,您的模型应该FK到City而不是状态),这个小部件包含两个Select字段,第一个包含Sstates,第二个包含一个State的选择(你将需要插入视图以返回基于状态ID填充您的城市选择)。

You need to create a custom widget to pick the City model (that is, your model should FK to City and not to State), this widget contains two Select fields, the first one contains the Sstates and the second one gets loaded on the pick of a State (you will need to plug a view to return Cities based on State ID to populate your City select).

您应该设置您的小部件Media内部类别指向特定的.js文件链接两个选择。

You should set your widgets Media inner class point to the specific .js file chaining both Selects.

在ModelAdmin规范中,将您的字段的小部件设置为刚刚创建的自定义窗口小部件,它的媒体将被自动添加到change_form模板。

In the ModelAdmin specification, set your field's widget to the custom widget you just created and it's media will be automatically added to the change_form template.

确保您的.js文件查找您的常规JQuery对象并返回到django.JQuery,这样您可以在管理员中使用相同的小部件,并通过您的站点。

Make sure that your .js file looks for your regular JQuery object and falls back to django.JQuery, this way you can use this same widget in the admin and through out your site.

(function($) {
// Note that this function works only for one widget per page
$('#state').change(function(){
    $('#city').load('/cities_by_state/', {id: this.value}); // the endpoint returns HTML
});
})(JQuery||django.JQuery);

我在我的项目本地使用的应用程序中做了类似的事情(可变深度最多三个级别),结果解决方案最终有点毛茸茸,因为它必须支持每页多个小部件,动态小部件(用于内联),模板标签以各种形式呈现小部件等。

I've done something similar in an app I use locally for my projects (variable depth up to three levels) and the resulting solution ended up a bit hairy as it had to support multiple widgets per page, dynamic widgets (for inlines), templatetags to render the widget in various forms, etc.

这篇关于Django管理员显示分层下拉式筛选器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆