处理链接模型字段的django应用程序。
django-smart-selects-fix的Python项目详细描述
django smart选择
当前未维护-如果要维护此应用程序,请与Digi604联系
此包允许您通过向模型中添加自定义外键或多对多字段来快速筛选或分组“链接”模型。这将使用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
表示应该链接到同一模型上的字段。在Continent
,Country
,Location
示例中,chained_field
是模型Location
中字段continent
的名称。
classLocation(models.Model)continent=models.ForeignKey(Continent)
链式模型字段(必需)
chained_model_field
表示链接模型的字段,该字段对应于由chained_field
链接的模型。在Continent
,Country
,Location
示例中,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)
在管理
中使用链接字段不要指定ModelAdmin
filter_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
表示应该链接到同一模型上的字段。在Publication
,Writer
,Book
示例中,chained_field
是模型Book
中字段publication
的名称。
classBook(models.Model):publication=models.ForeignKey(Publication)
chained_model_field
(必需)
chained_model_field
表示链接模型的字段,该字段对应于由chained_field
链接的模型。在Publication
,Writer
,Book
示例中,chained_model_field
是Writer
模型中字段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")
安装
将
smart_selects
添加到INSTALLED_APPS
将
smart_selects
url添加到项目的urls.py
中。这对于Chained Selects
和Chained ManyToMany Selects
是必需的。例如:urlpatterns=patterns('',url(r'^admin/',include(admin.site.urls)),url(r'^chaining/',include('smart_selects.urls')),)
您还需要在每个页面中包括来自
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。但是,它可以使用django的jquery
一德明区。设置USE_DJANGO_JQUERY = True
以启用此行为。
待办事项
- 添加权限检查以允许用户限制谁可以使用链接字段。
- 添加一个
ChainedCheckboxSelectMultiple
小部件并调整chainedm2m.js
和chainedfk.js
以在这种情况下生成复选框