我正在尝试使用自动完成灯支持过滤M2M查询集。 我可以让过滤器使用内置的ModelForm。以下是没有自动完成功能的完美代码的简化版本:
models.py:
class FEmodel(models.Model):
name = models.Charfield(max_length=200)
class Workspace(models.Model):
name = models.Charfield(max_length=200)
fem = models.ForeignKey(FEmodel)
class Element(models.Model):
EID = models.PositiveIntegerField()
fem = models.ForeignKey(FEmodel)
class Panel(models.Model):
workspace = models.ForeignKey(Workspace)
elements = models.ManyToManyField(Element)
forms.py:
class PanelForm(forms.ModelForm):
def __init__(self,*args,**kwargs):
super(PanelForm,self).__init__(*args,**kwargs)
ws = self.instance.workspace
self.fields['elements'].queryset = Element.objects.filter(fem=ws.fem)
class Meta:
model = Panel
fields = ('__all__')
views.py:
@login_required(login_url='/login/')
def panel_edit(request, pk, id=None):
workspace = get_object_or_404(Workspace, pk=pk)
if id:
panel = get_object_or_404(Panel, pk=id)
else:
panel = Panel(workspace = workspace)
if request.method == 'POST':
form = PanelForm(request.POST, instance=panel)
if form.is_valid():
panel = form.save(commit=True)
return panels(request, pk)
else:
print form.errors
else:
form = PanelForm(instance=panel)
return render(request, 'structures/Panel/panel_edit.html', {'form': form, 'panel': panel, 'workspace': workspace})
urls.py:
...
url(r'^workspace/(?P<pk>[0-9]+)/panel/new/$', views.panel_edit, name='panel_edit'),
...
panel_edit.html:
...
<form method="POST" class="form-horizontal">
{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
<button type="submit"> Save</button>
{% endbuttons %}
</form>
....
这是自动完成版本,我无法正常工作:
autocomplete_light_registry.py
class ElementAutocomplete(acl.AutocompleteModelBase):
search_fields = ['EID']
acl.register(Element, ElementAutocomplete)
forms.py:
import autocomplete_light.shortcuts as acl
class PanelForm(acl.ModelForm):
def __init__(self,*args,**kwargs):
super(PanelForm,self).__init__(*args,**kwargs)
ws = self.instance.workspace
self.fields['elements'].queryset = Element.objects.filter(fem=ws.fem)
class Meta:
model = Panel
fields = ('__all__')
此版本不会引发任何错误,但不会提供由form.instance.ws.fem属性过滤的元素选项。相反,它给出了所有Element对象。
我做错了什么?
编辑1:
编辑2:添加了网址,视图和模板的必要部分
编辑3: 根据@ jpic的回答,这里是解决方案:
已添加到panel_edit.html:
{% block bootstrap3_extra_head %}
{{ block.super }}
<script type="text/javascript">
$( document ).ready(function() {
elements_autocomplete = $('input[name=elements-autocomplete]').yourlabsAutocomplete()
elements_autocomplete.data['ws_pk'] = {{ form.instance.workspace.pk }}
});
</script>
{% endblock %}
autocomplete_light_registry.py:
import autocomplete_light as acl
class ElementAutocomplete(acl.AutocompleteModelBase):
search_fields = ['EID']
model = Element
def choices_for_request(self):
ws = Workspace.objects.get(pk=self.request.GET.get('ws_pk', None))
self.choices = self.choices.filter(fem=ws.fem)
return super(ElementAutocomplete, self).choices_for_request()
acl.register(ElementAutocomplete)
forms.py:
class PanelForm(acl.ModelForm):
class Meta:
model = Panel
fields = ('__all__')
答案 0 :(得分:1)
自动完成JS对象需要pk值将其传递给调用Python自动完成对象的视图,然后您可以在python自动完成对象的choices_for_request()方法中对实例pk进行过滤。
获取js自动完成对象的一种方法是get it from the input element itself with the jQuery plugin,即:
elements_autocomplete = $('input[name=elements-autocomplete]').yourlabsAutocomplete()
确保在加载 jquery-autocomplete-light JS之后将其称为。
elements_autocomplete.data['panel_pk'] = {{ form.instance.pk }}
在choices_for_request()中,你现在可能已经弄明白了:
def choices_for_request(self):
choices = super(ElementAutocomplete, self).choices_for_request()
panel_pk = request.GET.get('panel_pk', None)
if panel_pk and panel_pk.isdigit():
choices = choices.filter(panel__pk=panel_pk)
return choices
实际上,您可以在浏览器中轻松测试,打开JS控制台并运行:$('input[name=elements-autocomplete]').yourlabsAutocomplete().data['foo'] = 'bar'
,您将看到自动完成脚本发出的后续请求将添加{{ 1}}到它的URL,通过self.request将它提供给choices_for_request!