2017-09-27 57 views
1
customer = CustomerProfile.objects.get(pk=4) 
ipdb> SimilarCustomerFinder(self, customer=customer, fields=self.fields) 
*** TypeError: __init__() got multiple values for keyword argument 'customer' 

多個值在SimilarCustomerFinder類,我有類型錯誤:__init __()得到了關鍵字參數 '客戶'

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',爲什麼?

+3

此代碼沒有意義。第一個片段中的「self」是什麼? –

回答

3

鑑於ipdb輸出好像你正在嘗試使用此命令創建一個實例:

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

然而self是隱式傳遞參數,因此你不應該在明確地傳遞。就像這樣:

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

或者,如果你真的打算把它傳遞明確的(這將是很古怪,可能不會做你想要什麼 - 但誰知道...)你必須明確地調用該方法上課:

SimilarCustomerFinder.__init__(self, customer=customer, fields=self.fields) 
相關問題