django 智能选择 ajax 配置
django smart-selects ajax configuration
所以我终于在管理员端实现了 smart-selects (https://github.com/digi604/django-smart-selects),但是现在当我试图让实际的过滤器在用户端工作时,过滤器不工作。我试图研究解决这个问题的方法,似乎我需要实施 ajax 才能使智能 select 过滤器正常工作。我也确定我的 form.py 设置不正确,但我想不出调用 .objects.all() 的替代方法,因为我认为 smart-selects 会进行适当的过滤在后台。
我将包括我的 models.py 和 forms.py 以防万一解决方案就在其中。我以前从未使用过 ajax 脚本,我的研究也没有为我指明从哪里开始的任何方向。
models.py
from django.db import models
from django.contrib.auth.models import User
from smart_selects.db_fields import ChainedForeignKey
class Status(models.Model):
status = models.CharField(primary_key=True, max_length=25)
## For the API
def __str__(self):
return self.status
## Eliminates title plurality in the admin interface
class Meta:
verbose_name_plural="Status"
class Lab(models.Model):
name = models.CharField(primary_key=True, max_length=100)
## For the API
def __str__(self):
return self.name
## Eliminates title plurality in the admin interface
class Meta:
verbose_name_plural="Lab"
class Category(models.Model):
lab = models.ForeignKey(Lab)
name = models.CharField(primary_key=True, max_length=100)
## For the API
def __str__(self):
return self.name
## Eliminates title plurality in the admin interface
class Meta:
verbose_name_plural="Category"
class SubCategory(models.Model):
lab = models.ForeignKey(Lab)
category = ChainedForeignKey(
Category,
chained_field = 'lab',
chained_model_field = 'lab',
show_all = False,
auto_choose = True
)
subcategory = models.CharField(max_length=100)
category = models.ForeignKey(Category)
## For the API
#def __str__(self):
# return self.category
## Eliminates title plurality in the admin interface
class Meta:
verbose_name_plural="SubCategory"
forms.py
import datetime
from django import forms
## importing models
from .models import Category, Lab, SubCategory ## need these tables to populate dropdown menus 'Category' and 'Lab'
from submit_app.models import Incident
from smart_selects.db_fields import ChainedForeignKey
## importing crispy form utilities
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Field, Submit, Div
from crispy_forms.bootstrap import AppendedText, PrependedText, FormActions
## importing regex validator
from django.core.validators import RegexValidator
## Maybe best to try to use ModelForm....it seems to have overwhelming internet support
from django.forms import ModelForm
class IncidentForm(forms.ModelForm):
class Meta:
model = Incident
fields = [ 'date_occurred',
'number_of_samples_affected',
'capa',
'title',
'description',
'category',
'lab',
'subcategory',
'upload1',
'upload2',
'upload3'
]
## Pre-populated dropdown menu
lab = forms.ModelChoiceField(
queryset=Lab.objects.all(),
label ="Lab"
)
## Pre-populated dropdown menu
category = forms.ModelChoiceField(
queryset=Category.objects.all(),
label = "Category"
)
subcategory = forms.ModelChoiceField(
queryset=SubCategory.objects.all(),
label = "SubCategory"
)
date_occurred = forms.DateField(
label="Date Incident Occurred",
initial=datetime.date.today()
)
number_of_samples_affected = forms.IntegerField(
label="Number of Samples Affected",
initial='0'
)
smart selects
有自己的表单字段类型。您现在正在为 category
在您的表单中覆盖它:
## Pre-populated dropdown menu
category = forms.ModelChoiceField(
queryset=Category.objects.all(),
label = "Category"
)
从您的表单中删除上面的所有代码。当您使用 ChainedForeignKey
时,模型字段的默认表单字段类型由 smart selects
提供(请参阅源代码中的 ChainedModelChoiceField)。
执行 AJAX 调用的 Javascript 是一个小的内联脚本,作为 smart selects
提供的表单字段的一部分呈现。
您不必用任何东西替换代码,因为当包含在 ModelForm
.
中时,该字段会自动呈现
Ian Price 的反应很好。我试过了,效果很好。问题在于 jQuery 库的加载。它必须是要加载的所有标签之上的第一个
Base.html
<head>
<title>testing</title>
{% load static %}
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="{% static 'smart-selects/admin/js/chainedfk.js' %}"></script>
<script src="{% static 'smart-selects/admin/js/chainedm2m.js' %}"></script>
</head>
所以我终于在管理员端实现了 smart-selects (https://github.com/digi604/django-smart-selects),但是现在当我试图让实际的过滤器在用户端工作时,过滤器不工作。我试图研究解决这个问题的方法,似乎我需要实施 ajax 才能使智能 select 过滤器正常工作。我也确定我的 form.py 设置不正确,但我想不出调用 .objects.all() 的替代方法,因为我认为 smart-selects 会进行适当的过滤在后台。
我将包括我的 models.py 和 forms.py 以防万一解决方案就在其中。我以前从未使用过 ajax 脚本,我的研究也没有为我指明从哪里开始的任何方向。
models.py
from django.db import models
from django.contrib.auth.models import User
from smart_selects.db_fields import ChainedForeignKey
class Status(models.Model):
status = models.CharField(primary_key=True, max_length=25)
## For the API
def __str__(self):
return self.status
## Eliminates title plurality in the admin interface
class Meta:
verbose_name_plural="Status"
class Lab(models.Model):
name = models.CharField(primary_key=True, max_length=100)
## For the API
def __str__(self):
return self.name
## Eliminates title plurality in the admin interface
class Meta:
verbose_name_plural="Lab"
class Category(models.Model):
lab = models.ForeignKey(Lab)
name = models.CharField(primary_key=True, max_length=100)
## For the API
def __str__(self):
return self.name
## Eliminates title plurality in the admin interface
class Meta:
verbose_name_plural="Category"
class SubCategory(models.Model):
lab = models.ForeignKey(Lab)
category = ChainedForeignKey(
Category,
chained_field = 'lab',
chained_model_field = 'lab',
show_all = False,
auto_choose = True
)
subcategory = models.CharField(max_length=100)
category = models.ForeignKey(Category)
## For the API
#def __str__(self):
# return self.category
## Eliminates title plurality in the admin interface
class Meta:
verbose_name_plural="SubCategory"
forms.py
import datetime
from django import forms
## importing models
from .models import Category, Lab, SubCategory ## need these tables to populate dropdown menus 'Category' and 'Lab'
from submit_app.models import Incident
from smart_selects.db_fields import ChainedForeignKey
## importing crispy form utilities
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Field, Submit, Div
from crispy_forms.bootstrap import AppendedText, PrependedText, FormActions
## importing regex validator
from django.core.validators import RegexValidator
## Maybe best to try to use ModelForm....it seems to have overwhelming internet support
from django.forms import ModelForm
class IncidentForm(forms.ModelForm):
class Meta:
model = Incident
fields = [ 'date_occurred',
'number_of_samples_affected',
'capa',
'title',
'description',
'category',
'lab',
'subcategory',
'upload1',
'upload2',
'upload3'
]
## Pre-populated dropdown menu
lab = forms.ModelChoiceField(
queryset=Lab.objects.all(),
label ="Lab"
)
## Pre-populated dropdown menu
category = forms.ModelChoiceField(
queryset=Category.objects.all(),
label = "Category"
)
subcategory = forms.ModelChoiceField(
queryset=SubCategory.objects.all(),
label = "SubCategory"
)
date_occurred = forms.DateField(
label="Date Incident Occurred",
initial=datetime.date.today()
)
number_of_samples_affected = forms.IntegerField(
label="Number of Samples Affected",
initial='0'
)
smart selects
有自己的表单字段类型。您现在正在为 category
在您的表单中覆盖它:
## Pre-populated dropdown menu
category = forms.ModelChoiceField(
queryset=Category.objects.all(),
label = "Category"
)
从您的表单中删除上面的所有代码。当您使用 ChainedForeignKey
时,模型字段的默认表单字段类型由 smart selects
提供(请参阅源代码中的 ChainedModelChoiceField)。
执行 AJAX 调用的 Javascript 是一个小的内联脚本,作为 smart selects
提供的表单字段的一部分呈现。
您不必用任何东西替换代码,因为当包含在 ModelForm
.
Ian Price 的反应很好。我试过了,效果很好。问题在于 jQuery 库的加载。它必须是要加载的所有标签之上的第一个
Base.html
<head>
<title>testing</title>
{% load static %}
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="{% static 'smart-selects/admin/js/chainedfk.js' %}"></script>
<script src="{% static 'smart-selects/admin/js/chainedm2m.js' %}"></script>
</head>