【发布时间】:2018-02-14 03:31:33
【问题描述】:
请在我的代码末尾查看我的编辑。
我正在尝试为单个字段实现 django-autocomplete-light (dal 3.2.10)。按照教程,我发现了这个错误:'list' object has no attribute 'queryset'。
我看过这个问题:django-autocomplete-light error = 'list' object has no attribute 'queryset'。它没有解决我的问题。
为什么会出现这个错误?我能做些什么来解决这个问题?
我不认为这是整个问题,但我没有看到任何 js 文件出现在浏览器检查器中。我认为在 Edit #3 中包含代码会导致出现一些问题。
我有两个模型:
class Entity(models.Model):
entity = models.CharField(primary_key=True, max_length=12)
entityDescription = models.CharField(max_length=200)
def __str__(self):
return self.entityDescription
class Action(models.Model):
entity = models.ForeignKey(Entity, on_delete=models.CASCADE, db_column='entity')
entityDescription = models.CharField(max_length=200)
action = models.CharField(max_length=50)
def __str__(self):
return '%s' % self.entity
我有一个模型表单和表单集。我也在使用crispy-forms 来渲染表单集:
class ActionForm(ModelForm):
class Meta:
model = Action
fields = '__all__'
widgets = {
'entityDescription': autocomplete.ModelSelect2(url='eda')
}
ActionFormSet = modelformset_factory(Action, extra=1, exclude=(), form=ActionForm)
我有一个看法:
class EntityDescriptionAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
qs = Entity.objects.all()
if self.q:
qs = qs.filter(entityDescription__istartswith=self.q)
return qs
我有一个 urls.py:
urlpatterns = [
url(
r'^eda/$',
views.EntityDescriptionAutocomplete.as_view(),
name='eda',
),
]
感谢大家提供的任何见解。
编辑:
我变了……
widgets = {
'entityDescription': autocomplete.ModelSelect2(url='eda'),
}
...到...
widgets = {
'entityDescription': autocomplete.Select2(url='eda'),
}
...这允许我的页面呈现,但自动完成字段是一个空的下拉菜单。为什么是空的,为什么不是自动完成框?
编辑#2:
我删除了元类中的小部件设置,而是直接覆盖了该字段:
class ActionForm(ModelForm):
entityDescription = ModelChoiceField(
queryset=Entity.objects.all(),
widget=autocomplete.ModelSelect2(url='eda')
)
class Meta:
model = Action
fields = '__all__'
这仍然返回一个空的下拉列表(不是自动完成框),但它现在有 Django 的 ------- 符号而不是完全没有。
编辑#3:
我将此添加到我的模板中:
{% block footer %}
<script type="text/javascript" src="/static/collected/admin/js/vendor/jquery/jquery.js"></script>
{{ form.media }}
{% endblock %}
没有任何改变。
【问题讨论】:
标签: python django python-3.x autocomplete django-autocomplete-light