python - 获取 self.fields 中的多个值

标签 python django

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/

相关文章:

python - 升级后无法加载 GDAL 库

django - 如何从 django-celery 3 任务发送 channel 2.x 组消息?

python - Django 模型 : Keep track of activity through related models?

python - 多重继承 - 为什么输出不符合 MRO

python - 如何在 ML 分类中处理字符串数据

Django postgres HStoreField 排序依据

python - 如何将 self 传递给单独类的未绑定(bind)方法

python - celery 任务消失 - Django/Celery

python - numpy.polyfit 的额外结果是什么意思?

python - SUMIF 类似于 Pandas 中的函数