Django Admin级联选择(Cascader)包django-smart-selects

2024-04-1509:26:56后端程序开发Comments241 views字数 3129阅读模式

级联选择(Cascader)是一种选择控件,以分组菜单的形式展示选项,通常包含两个或以上的层级。它适合于有逻辑顺序的场景,如“省、市、区”的选择。Django下django-smart-selects提供级联选择应用。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/63109.html

使用场景:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/63109.html

  • 常用于单选,但也可以支持多选。
  • 适用于选项之间存在明显的层级关系,如上下级关系或权限的包含关系。
  • 与输入框配合使用,可以通过下拉菜单承载。

1.安装django-smart-selects文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/63109.html

pip install django-smart-selects

2.在settings.py的INSTALLED_APPS添加smart_selects文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/63109.html

3.添加urls路由文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/63109.html

urlpatterns = patterns('',    url(r'^admin/', include(admin.site.urls)),    url(r'^chaining/', include('smart_selects.urls')),)

4.链式选择示例应文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/63109.html

models.py文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/63109.html

class Continent(models.Model):    name = models.CharField(max_length=255)
class Country(models.Model):    continent = models.ForeignKey(Continent)    name = models.CharField(max_length=255)
class Location(models.Model):    continent = models.ForeignKey(Continent)    country = models.ForeignKey(Country)    area = models.ForeignKey(Area)    city = models.CharField(max_length=50)    street = models.CharField(max_length=100)

选择大洲Location后,如果只想该大洲上的国家/地区可用,则可以在模型上使用 ChainedForeignKey文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/63109.html

from smart_selects.db_fields import ChainedForeignKey
class Location(models.Model):    continent = models.ForeignKey(Continent)    country = ChainedForeignKey(        Country,        chained_field="continent",        chained_model_field="continent",        show_all=False,        auto_choose=True,        sort=True)    area = ForeignKey(Area)    city = models.CharField(max_length=50)    street = models.CharField(max_length=100)

ChainedForeignKey选项文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/63109.html

  • chained_field (必填)表示同一模型上应链接到的字段。示例是模型中字段的名称。
  • chained_model_field(必填)表示与Model链接的模型相对应的链式模型的字段。示例Model中字段的名称。
  • show_all(可选)指只显示筛选的结果,还是还想进一步显示其他结果。
  • auto_choose(可选)是否在只有一个可用选项时自动选择选项。
  • sort排序(可选)指是否应按字典对结果集进行排序。默认Model.ordering为True 。

5.链接 ManyToMany 选择示例文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/63109.html

models.py文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/63109.html

from smart_selects.db_fields import ChainedManyToManyField
class Publication(models.Model):    name = models.CharField(max_length=255)
class Writer(models.Model):    name = models.CharField(max_length=255)    publications = models.ManyToManyField('Publication', blank=True, null=True)
class Book(models.Model):    publication = models.ForeignKey(Publication)    writer = ChainedManyToManyField(        Writer,        chained_field="publication",        chained_model_field="publications")    name = models.CharField(max_length=255)
from smart_selects.db_fields import ChainedManyToManyField
class Publication(models.Model):    name = models.CharField(max_length=255)
class Writer(models.Model):    name = models.CharField(max_length=255)    publications = models.ManyToManyField('Publication', blank=True, null=True)
class Book(models.Model):    publication = models.ForeignKey(Publication)    writer = ChainedManyToManyField(        Writer,        horizontal=True,        verbose_name='writer',        chained_field="publication",        chained_model_field="publications")    name = models.CharField(max_length=255)

ChainedManyToManyField 选项
chained_field(必填)表示同一模型上应链接到的字段。示例是模型中字段的名称。
chained_model_field(必填)表示与链接的模型相对应的链式模型的字段。示例是模型中字段的名称。
auto_choose(可选)指是否在只有一个可用选项时自动选择选项。
horizontal(可选)
混合 Django 的,以便在 Django 后台使用FilteredSelectMultiple文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/63109.html

6.在模板中的使用文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/63109.html

在模板中,像往常一样继续使用 Django 表单或模型表单。只需包含在您的表格中即可。这将自动将以下 Javascript 文件添加到您的页面中:{{ form.media.js }}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/63109.html

<script src="/static/smart-selects/admin/js/chainedfk.js"></script><script src="/static/smart-selects/admin/js/bindfields.js"></script>
下面是一个基本的用法示例:
<form method="post">    {% csrf_token %}    {{ form.media.js }}    {{ form.as_p }}    <input type="submit" value="Submit" /></form>

参考文档文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/63109.html

项目github:https://github.com/jazzband/django-smart-selects文档:https://django-smart-selects.readthedocs.io/en/latest/installation.html
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/63109.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/63109.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定