缺少 1 个必需的位置参数:'queryset'

Posted

技术标签:

【中文标题】缺少 1 个必需的位置参数:\'queryset\'【英文标题】:missing 1 required positional argument: 'queryset'缺少 1 个必需的位置参数:'queryset' 【发布时间】:2017-03-27 07:03:18 【问题描述】:

我正在尝试在 admin.py 操作中更新扩展用户模型 profile。我已经研究了几个小时了,但结果很短。我收到pc_add_1() missing 1 required positional argument: 'queryset' 错误,请帮忙。

class ProfileAdminInLine(admin.StackedInline):
    model = Profile


class ProfileAdmin(UserAdmin):
    list_display = ['username', 'email', 'first_name', 'last_name', 'is_staff',
                    'rewards_punch_card', 'rewards_tier', 'credits']
    list_select_related = True
    inlines = [ProfileAdminInLine]

    actions = ['pc_add_1', 'pc_add_2', 'pc_add_3', 'pc_add_4', 'pc_add_5',
               'pc_add_6', 'pc_add_7', 'pc_add_8', 'pc_add_9']

    def rewards_tier(self, user):
        return user.profile.rewards_tier

    def rewards_punch_card(self, user):
        return user.profile.rewards_current

    def pc_add_1(self, request, user, queryset):

        punch_card = user.profile.rewards_current
        tier = user.profile.rewards_tier
        credits = user.profile.rewards_credits

        punch_cards_updated = queryset.update(punch_card + 1)

        if punch_cards_updated == 10:
            queryset.update(punch_card == 0)
            if tier == 1:
                queryset.update(tier + 1)
                queryset.update(credits + 25)
            elif tier == 2:
                queryset.update(tier + 1)
                queryset.update(credits + 35)
            elif tier == 3:
                queryset.update(tier + 1)
                queryset.update(credits + 45)
            elif tier == 4:
                queryset.update(tier + 1)
                queryset.update(credits + 55)
            elif tier == 5:
                queryset.update(credits + 65)
            elif tier == 6:
                queryset.update(credits + 65)

        else:
            pass

追溯

Internal Server Error: /admin/auth/user/
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 39, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.5/site-packages/django/contrib/admin/options.py", line 544, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/django/contrib/admin/sites.py", line 211, in inner
    return view(request, *args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/django/utils/decorators.py", line 67, in _wrapper
    return bound_func(*args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/django/utils/decorators.py", line 63, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "/usr/local/lib/python3.5/site-packages/django/contrib/admin/options.py", line 1569, in changelist_view
    response = self.response_action(request, queryset=cl.get_queryset(request))
  File "/usr/local/lib/python3.5/site-packages/django/contrib/admin/options.py", line 1305, in response_action
    response = func(self, request, queryset)
TypeError: pc_add_1() missing 1 required positional argument: 'queryset'
[13/Nov/2016 15:16:52] "POST /admin/auth/user/ HTTP/1.1" 500 106710

【问题讨论】:

请将完整的错误回溯添加到您的问题中! 已更新,对此感到抱歉! 【参考方案1】:

你的方法签名错误:

def pc_add_1(self, request, user, queryset):

对于管理员操作应该是

def pc_add_1(self, request, queryset):

现在,如果您想知道是哪个管理员用户进行了更改,您可以从 request.user 找到它

【讨论】:

【参考方案2】:

您必须从该行中删除参数user

def pc_add_1(self, request, user, queryset):

Django 将仅使用requestqueryset 调用该方法。您将能够通过request.user 获取用户。

【讨论】:

以上是关于缺少 1 个必需的位置参数:'queryset'的主要内容,如果未能解决你的问题,请参考以下文章

“fit() 缺少 1 个必需的位置参数:'y'”错误

TypeError:缺少 1 个必需的位置参数:'self'

类型错误:parse() 缺少 1 个必需的位置参数:'stream'

TypeError:AuthMiddlewareStack() 缺少 1 个必需的位置参数:'inner'

类型错误:预测()缺少 1 个必需的位置参数:'params'

addChild() 缺少 1 个必需的位置参数:'pk