处理链接模型字段的django应用程序。

django-smart-selects-711的Python项目详细描述


django smart选择

Build Status

Coverage Status

这个包是从官方的django smart selects repo派生的,并应用了所有的bug修复。

此包允许您通过向模型中添加自定义外键或多对多字段来快速筛选或分组“链接”模型。这将使用ajax查询只加载适用的链接对象。

警告:默认情况下,ajax端点不强制执行任何权限。这意味着任何具有链接字段的模型都将是世界可读的。如果您希望对此权限进行更多控制,^{}包是一个很好的高质量包,它通过权限检查启用相同的功能。

链式选择

给定以下模型:

classContinent(models.Model):name=models.CharField(max_length=255)classCountry(models.Model):continent=models.ForeignKey(Continent)name=models.CharField(max_length=255)classLocation(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

fromsmart_selects.db_fieldsimportChainedForeignKeyclassLocation(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)

链外键选项

链式字段(必需)

chained_field表示应该链接到同一模型上的字段。在ContinentCountryLocation示例中,chained_field是模型Location中字段continent的名称。

classLocation(models.Model)continent=models.ForeignKey(Continent)

链式模型字段(必需)

chained_model_field表示链接模型的字段,该字段对应于由chained_field链接的模型。在ContinentCountryLocation示例中,chained_model_field是模型Country中字段continent的名称。

classCountry(models.Model):continent=models.ForeignKey(Continent)

全部显示(可选)

show_all指示是否只应显示筛选的结果,或者是否还希望进一步向下显示其他结果。

自动选择(可选)

auto_choose表示只有一个可用选项时是否自动选择该选项。

sort(可选)

^ }表示结果集是否应按词典编排或不排序。如果要使用Model.ordering选项,请禁用。默认为True

链式多人选择

ChainedManyToManyField的工作方式与您预期的一样:

fromsmart_selects.db_fieldsimportChainedManyToManyFieldclassPublication(models.Model):name=models.CharField(max_length=255)classWriter(models.Model):name=models.CharField(max_length=255)publications=models.ManyToManyField('Publication',blank=True,null=True)classBook(models.Model):publication=models.ForeignKey(Publication)writer=ChainedManyToManyField(Writer,chained_field="publication",chained_model_field="publications")name=models.CharField(max_length=255)

在管理

中使用链接字段

不要指定ModelAdminfilter_horizontal列表中的字段。相反,只需将horizontal=True传递给ChainedManyToManyField

fromsmart_selects.db_fieldsimportChainedManyToManyFieldclassPublication(models.Model):name=models.CharField(max_length=255)classWriter(models.Model):name=models.CharField(max_length=255)publications=models.ManyToManyField('Publication',blank=True,null=True)classBook(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)

链式多个主字段选项

chained_field(必需)

chained_field表示应该链接到同一模型上的字段。在PublicationWriterBook示例中,chained_field是模型Book中字段publication的名称。

classBook(models.Model):publication=models.ForeignKey(Publication)

chained_model_field(必需)

chained_model_field表示链接模型的字段,该字段对应于由chained_field链接的模型。在PublicationWriterBook示例中,chained_model_fieldWriter模型中字段publications的名称。

classWriter(models.Model):publications=models.ManyToManyField('Publication',blank=True,null=True)

auto_choose(可选)

auto_choose表示只有一个可用选项时是否自动选择该选项。

horizontal(可选)

< P>此选项将混合django的^ {CD50>}以在django管理员中工作,正如您预期的

分组选择

如果您有以下型号:

classCountry(models.Model):continent=models.ForeignKey(Continent)classLocation(models.Model):continent=models.ForeignKey(Continent)country=models.ForeignKey(Country)

如果要在html选择列表中按大陆对国家进行分组,可以使用GroupedForeignKey

fromsmart_selects.db_fieldsimportGroupedForeignKeyclassLocation(models.Model):continent=models.ForeignKey(Continent)country=GroupedForeignKey(Country,"continent")

安装

  1. smart_selects添加到INSTALLED_APPS

  2. smart_selectsurl添加到项目的urls.py中。这对于Chained SelectsChained ManyToMany Selects是必需的。例如:

    urlpatterns=patterns('',url(r'^admin/',include(admin.site.urls)),url(r'^chaining/',include('smart_selects.urls')),)
  3. 您还需要在每个页面中包括来自smart_selects的字段,或者在项目的settings.py中设置JQUERY_URL = True

设置

JQUERY_URL :jquery 2.2.0如果设置为True,则从google的cdn加载。如果你愿意的话 使用其他版本将完整的url放在此处。设置JQUERY_URL = False 完全禁用加载jquery。

USE_DJANGO_JQUERY :默认情况下,smart_selects从google的cdn加载jquery。但是,它可以使用jquery来自Django's 管理区。设置USE_DJANGO_JQUERY = True以启用此行为。

待办事项

  • 添加权限检查以允许用户限制谁可以使用链接字段。
  • 添加一个ChainedCheckboxSelectMultiple小部件并调整chainedm2m.jschainedfk.js以在这种情况下生成复选框

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何忽略缓冲读取器中在“”之后的行的其余部分,或行上的特定字符?   java在db中创建空对象或稍后保存   java如何实现UI无关的后台工作任务   java未能在Android中从BaseAdapter扩展的类中启动Tactivity?   java斐波那契迭代移动数组[]   安卓从文件读取提供了java。木卫一。StreamCorruptedException:无效的流标头:73720027   java计算矩形中的六边形数?   仅使用Java 1.5(或更早版本)读写XML   java如果所有元素都以相同的bucked结尾,为什么要进行大小调整?   java Apache POI Excel在xx中发现无法读取的内容。xlsx   swing我可以在普通java应用程序中使用GWTGUI吗?   来自自定义Java客户端的http删除请求的行为与邮递员不同   运行批处理文件时,java当前目录无效   使用TypeReference将java字符串转换为ArrayList<STRING>   documentlistener突出显示所有匹配词Java