Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        2年前关闭。
                                                                                            
                
        
fields = ['first_name', 'last_name', 'birth_date']
user__first_name, user__last_name, birth_date = self.fields.get(fields)


由于self.fields是词典,因此无法执行此操作。如何修改它使其起作用?

    198     def check_user__first_name_user__last_name_birth_date(self):
    199         import ipdb; ipdb.set_trace()
    200
--> 201         fields = ['first_name', 'last_name', 'birth_date']
    202         user__first_name, user__last_name, birth_date = self.fields.get(fields)
    203         for m in self._check(CustomerProfile, user__first_name=user__first_name,
    204                              user__last_name=user__last_name,
    205                              birth_date=birth_date):
    206             self._match(m.user, _('Same first name, last name and birth date.'),
    207                         fields, ', '.join(map(str, self.fields.get(fields))))

最佳答案

由于self.fields是字典,因此您需要的内容等同于:

user__first_name, user__last_name, birth_date = (self.fields.get("first_name"),
                                                 self.fields.get("last_name"),
                                                 self.fields.get("birth_date"))


因此,例如,使用列表推导:

fields = ['first_name', 'last_name', 'birth_date']
user__first_name, user__last_name, birth_date = [self.fields.get(x) for x in fields]

关于python - 在self.fields中获取多个值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46472649/

10-14 00:59