gpt4 book ai didi

python - Django list_filter查询集返回,ValueError : too many values to unpack

转载 作者:行者123 更新时间:2023-11-28 18:38:32 28 4
gpt4 key购买 nike

我正在站点管理应用程序 django 中构建一个 self 过滤器,用于在数组字段中包含指定值的用户。但我无法实现导致返回错误。

这是我的模型:

class Profile(Model):
"""
Profile
"""
code = CharField(
max_length = 32,
verbose_name = (_(u"Código")),
blank = True,
)
user = ForeignKey(User,
verbose_name = (_('Usuario')),
related_name = 'profile',
blank = False,
unique = True,
)
country = CountryField(
verbose_name = (_(u"País")),
blank = False,
default = 'CO',
)
preferences = CharField(
max_length = 500,
verbose_name = (_(u"Preferencias")),
blank = True,
)
etc...

这是我的那个模型的管理员注册:

class ProfileAdmin(ExportMixin, ModelAdmin):

list_display = (
'id',
'user',
'first_name',
'last_name',
'country',
'isTeacher',
'purchased',
'purchasedDetails',
'referred',
'moneySpent',
'likesCount',
'created',
'preferences',
)

list_filter = (
'country',
'referred',
PreferencesFilter,
)

search_fields = (
'user__username',
'user__first_name',
'user__last_name',
)
site.register(Profile, ProfileAdmin)

PreferencesFilter 函数在哪里:

class PreferencesFilter(SimpleListFilter):
title = _('Preferencias')
parameter_name = 'preferencias'

def lookups(self, request, model_admin):
return [PREFERENCES1 + PREFERENCES2]

def queryset(self, request, queryset):
if not self.value():
return queryset


return queryset.filter(preferences__contains=[self.value()])

PREFERENCES1 和 PREFERENCES2 的值为:

PREFERENCES1 = (
('Croché', 'Croché'),
('Patchwork', 'Patchwork'),
('Muñecos decorativos', 'Muñecos decorativos'),
('Tejido dos agujas', 'Tejido dos agujas')
)

PREFERENCES2 = (
('Arreglos florales', 'Arreglos florales'),
('Peinados', 'Peinados'),
('Decoración de uñas', 'Decoración de uñas')
)

但是当我使用该过滤器进行搜索时,应用会返回此堆栈跟踪:

 Template error:
In template /env/local/lib/python2.7/site-packages/suit/templates/admin/search_form.html, error at line 17
too many values to unpack
7 : {% if cl.search_fields %}
8 : <input type="text" size="40" name="{{ search_var }}" value="{{ cl.query }}" id="searchbar" placeholder="{% block search_placeholder %}{% trans 'keyword'|capfirst %}{% endblock %}"{{ cl.query|yesno:' class=active,' }}>&nbsp;
9 : <script type="text/javascript">document.getElementById("searchbar").focus();</script>
10 : {% endif %}
11 : {% spaceless %}
12 : {% if cl.has_filters %}
13 : {% block filters %}
14 : {% if cl.has_filters %}
15 : <span class="search-filters">
16 : {% for spec in cl.filter_specs %}
17 : {% suit_list_filter_select cl spec %}
18 : &nbsp;
19 : {% endfor %}
20 : </span>
21 : {% endif %}
22 : {% endblock %}
23 : {% endif %}
24 : {% endspaceless %}
25 :
26 : <input type="submit" value="{% trans 'Search' %}" class="btn btn-info"/>
27 : &nbsp;

Traceback:
File "/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
137. response = response.render()
File "/env/local/lib/python2.7/site-packages/django/template/response.py" in render
105. self.content = self.rendered_content
File "/env/local/lib/python2.7/site-packages/django/template/response.py" in rendered_content
82. content = template.render(context)
File "/env/local/lib/python2.7/site-packages/django/template/base.py" in render
140. return self._render(context)
File "/env/local/lib/python2.7/site-packages/django/template/base.py" in _render
134. return self.nodelist.render(context)
File "/env/local/lib/python2.7/site-packages/django/template/base.py" in render
840. bit = self.render_node(node, context)
File "/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
78. return node.render(context)
File "/env/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
123. return compiled_parent._render(context)
File "/env/local/lib/python2.7/site-packages/django/template/base.py" in _render
134. return self.nodelist.render(context)
File "/env/local/lib/python2.7/site-packages/django/template/base.py" in render
840. bit = self.render_node(node, context)
File "/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
78. return node.render(context)
File "/env/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
123. return compiled_parent._render(context)
File "/env/local/lib/python2.7/site-packages/django/template/base.py" in _render
134. return self.nodelist.render(context)
File "/env/local/lib/python2.7/site-packages/django/template/base.py" in render
840. bit = self.render_node(node, context)
File "env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
78. return node.render(context)
File "/home/kperez/Desarrollo/www.pleisi.com/env/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
123. return compiled_parent._render(context)
File "/env/local/lib/python2.7/site-packages/django/template/base.py" in _render
134. return self.nodelist.render(context)
File "/env/local/lib/python2.7/site-packages/django/template/base.py" in render
840. bit = self.render_node(node, context)
File "/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
78. return node.render(context)
File "/env/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
62. result = block.nodelist.render(context)
File "/env/local/lib/python2.7/site-packages/django/template/base.py" in render
840. bit = self.render_node(node, context)
File "/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
78. return node.render(context)
File "/env/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
62. result = block.nodelist.render(context)
File "/env/local/lib/python2.7/site-packages/django/template/base.py" in render
840. bit = self.render_node(node, context)
File "/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
78. return node.render(context)
File "/env/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
62. result = block.nodelist.render(context)
File "/env/local/lib/python2.7/site-packages/django/template/base.py" in render
840. bit = self.render_node(node, context)
File "/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
78. return node.render(context)
File "/env/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
62. result = block.nodelist.render(context)
File "/env/local/lib/python2.7/site-packages/django/template/base.py" in render
840. bit = self.render_node(node, context)
File "/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
78. return node.render(context)
File "/env/local/lib/python2.7/site-packages/django/template/base.py" in render
1220. return self.nodelist.render(new_context)
File "/env/local/lib/python2.7/site-packages/django/template/base.py" in render
840. bit = self.render_node(node, context)
File "/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
78. return node.render(context)
File "/env/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
305. return nodelist.render(context)
File "/env/local/lib/python2.7/site-packages/django/template/base.py" in render
840. bit = self.render_node(node, context)
File "/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
78. return node.render(context)
File "/env/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
389. return strip_spaces_between_tags(self.nodelist.render(context).strip())
File "/env/local/lib/python2.7/site-packages/django/template/base.py" in render
840. bit = self.render_node(node, context)
File "/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
78. return node.render(context)
File "/env/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
305. return nodelist.render(context)
File "env/local/lib/python2.7/site-packages/django/template/base.py" in render
840. bit = self.render_node(node, context)
File "/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
78. return node.render(context)
File "/env/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
53. result = self.nodelist.render(context)
File "/env/local/lib/python2.7/site-packages/django/template/base.py" in render
840. bit = self.render_node(node, context)
File "/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
78. return node.render(context)
File "/env/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
305. return nodelist.render(context)
File "/env/local/lib/python2.7/site-packages/django/template/base.py" in render
840. bit = self.render_node(node, context)
File "/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
78. return node.render(context)
File "/env/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
196. nodelist.append(node.render(context))
File "/env/local/lib/python2.7/site-packages/django/template/base.py" in render
1125. return func(*resolved_args, **resolved_kwargs)
File "/env/local/lib/python2.7/site-packages/suit/templatetags/suit_list.py" in suit_list_filter_select
113. choices = list(spec.choices(cl))
File "/env/local/lib/python2.7/site-packages/django/contrib/admin/filters.py" in choices
103. for lookup, title in self.lookup_choices:

Exception Type: ValueError at /site/accounts/profile/
Exception Value: too many values to unpack

我做错了什么,或者我应该怎么做才能解决这个问题。

最佳答案

我没有使用自定义过滤器,但我猜是行

 return queryset.filter(preferences__contains=[self.value()])

导致问题

contains 应该是单个值,而不是数组。你试过了吗?

return queryset.filter(preferences__contains=self.value())

编辑:

好的,我现在看到问题了:

您正在尝试附加两个元组以获取列表。元组是不可变的,您使用的语法不会将它们连接到列表中。

将查找更改为:

def lookups(self, request, model_admin):

newlist = list(PREFERENCES1)
newlist.extend( list(PREFERENCES2))
return newlist

关于python - Django list_filter查询集返回,ValueError : too many values to unpack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29930190/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com