Django list_filter查询集返回,ValueError:要解压缩的值太多

时间:2015-04-28 21:23:35

标签: python django

我正在网站管理员应用程序django中构建一个自我过滤器,用于在数组字段中包含especified值的用户。但是我无法实现原因返回错误。

这是我的模特:

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

我做得不好,或者我应该怎样做才能解决这个问题。

1 个答案:

答案 0 :(得分:1)

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

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

导致问题

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

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

编辑:

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

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

将查找更改为:

def lookups(self, request, model_admin):

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