【发布时间】:2017-03-20 10:42:22
【问题描述】:
我在 Django 中有以下管理命令,它使用来自外部源的数据更新记录:
class Command(BaseCommand):
def handle(self, *args, **options):
field_mappings = {
'first_name': 'Voornaam',
'initials': 'Voorletters',
'last_name_prefix': 'Voorvoegsel',
'last_name': 'Achternaam',
'sex': 'Geslacht',
'DOB': ['Geboortedatum', 'convert_DOB'],
'street': 'Straat',
'house_number': 'Huisnummer',
'zipcode': 'Postcode',
'city': 'Woonplaats',
'country': 'Land',
'phone_number': 'Telefoonnummer',
'phone_number_mobile': 'MobielTelefoonnummer',
'email_address': 'Emailadres',
'insurer_name': 'NaamVerzekeraar',
'insurance_policy_number': 'PolisnummerVerzekering',
'gp_name': 'NaamHuisarts',
}
patients = Patient.objects.all()
for patient in patients:
result = Query(patient.pharmacy, 'patient_by_bsn', {'BSN': patient.BSN}).run()
for x, y in field_mappings.items():
if type(y) == list:
pass
else:
setattr(patient, x, result[y]['0'])
patient.save()
print('Patient {}-{} updated'.format(patient.pharmacy.vv_id, patient.vv_id))
@staticmethod
def convert_DOB(value):
return timezone.datetime.utcfromtimestamp(value).date()
大多数字段无需先转换数据即可保存,但某些字段(如 DOB)需要转换(在本例中从 UNIX 时间戳转换为 Python datetime.date)。当前在if type(y) == list 下方显示pass 的位置我想首先通过列出的函数运行该值,以便保存convert_DOB(value) 而不是原始值-我该怎么做?
【问题讨论】:
标签: python django python-3.6