TypeError:__ init __()得到关键字参数'customer'的多个值

时间:2017-09-27 19:51:58

标签: python

customer = CustomerProfile.objects.get(pk=4)
ipdb> SimilarCustomerFinder(self, customer=customer, fields=self.fields)
*** TypeError: __init__() got multiple values for keyword argument 'customer'

SimilarCustomerFinder课程中,我有

def __init__(self, customer, fields):
    self._matches = {}
    props = self.__class__.__dict__.keys()
    self.customer = customer
    self.fields = fields
    self.checks = [k for k in props if k.startswith('check_')]
    if customer:
        self.user_id = customer.user.pk
    else:
        self.user_id = -1

    for check in self.checks:
        c = check.replace('+', '_')
        getattr(self, c)()

我正在努力解决这个错误。我该怎么办呢?如果我删除customer=customer,我会*** AttributeError: 'CustomerUpdateForm' object has no attribute 'user',为什么?

1 个答案:

答案 0 :(得分:3)

鉴于ipdb输出,您似乎正在尝试使用此命令创建实例:

SimilarCustomerFinder(self, customer=customer, fields=self.fields)

但是self是一个隐式传递的参数,所以你不应该明确地传递它。像这样:

SimilarCustomerFinder(customer=customer, fields=self.fields)

或者如果你真的打算明确地传递它(这可能很奇怪,可能不会按照你的意图 - 但谁知道......)你必须在类上显式调用该方法:

SimilarCustomerFinder.__init__(self, customer=customer, fields=self.fields)