Django error: got multiple values for keyword argument

前端 未结 3 1554
你的背包
你的背包 2020-12-31 00:51

I get the following error when instantiating a Django form with a the constructor overriden:

__init__() got multiple values for keyword argument \'collection         


        
3条回答
  •  灰色年华
    2020-12-31 01:41

    You're passing the collection_type argument in as a keyword argument, because you specifically say collection_type=collection_type in your call to the form constructor. So Python includes it within the kwargs dictionary - but because you have also declared it as a positional argument in that function's definition, it attempts to pass it twice, hence the error.

    However, what you're trying to do will never work. You can't have user=None, parent=None before the *args dictionary, as those are already kwargs, and args must always come before kwargs. The way to fix it is to drop the explicit definition of collection_type, user and parent, and extract them from kwargs within the function:

    def __init__(self, *args, **kwargs):
        collection_type = kwargs.pop('collection_type', None)
        user = kwargs.pop('user', None)
        parent = kwargs.pop('parent', None)
    

提交回复
热议问题