繁体   English   中英

AttributeError: 'dict' object 没有属性 'parseArrivalDate'

[英]AttributeError: 'dict' object has no attribute 'parseArrivalDate'

我正在尝试将 API 中的字段保存在我数据库中他的匹配字段中,但出现错误: AttributeError: 'dict' object has no attribute 'parseArrivalDate'

首先,我将它的类型从字符串转换为日期时间,我打印以查看类型,output 是: <class 'datetime.datetime'>

但是,当我尝试将parseArrivalDate的值也与 departmentDate 的值相同(我试图保存其中的第一个,然后将创建另一个)保存到数据库中时,我收到了错误。 有人可以说为什么会这样吗?

我还尝试了不同的方法来将它转换为我发现和研究的所有内容,如 make_aware 等,但没有成功保存。

from datetime import datetime

arrivalDate = response.json()['bookings'][0]['arrival']['date']
        print(arrivalDate)
        parseArrivalDate = datetime.strptime(arrivalDate, '%Y-%m-%d')
        print(type(parseArrivalDate))
    
        departureDate = response.json()['bookings'][0]['departure']['date']
    
        patientIdWebFlow = response.json()['patient']['patient_id']
    
        patientsIdDB = Patient.objects.values('coreapi_id')

#here I loop through the id's and check if the record from API match  with some of the records in my database and tried to save the date for this id.
        for patientIdDB in patientsIdDB:
            if patientIdWebFlow == int(patientIdDB['coreapi_id']):
                print('they matched')
                datesPatients, created = Patient.objects.get_or_create(
                        coreapi_id=int(patientIdDB['coreapi_id']),
                        defaults=dict(
                            date_arrival=patientIdDB.parseArrivalDate,
                            date_departure=patientIdDB.departureDate,
                        ),
                    )
            else:
                print('they not matched')

我的模型.py:

class Patient(models.Model):
    coreapi_id = models.CharField(max_length=100)
    date_arrival = models.DateField(null=True)
    date_departure = models.DateField(null=True)
defaults=dict(
    date_arrival=patientIdDB.parseArrivalDate,
    date_departure=patientIdDB.departureDate,
),

您正在访问parseArrivalDate patientIdDB (an instance of Parent) parseArrivalDate 属性,它不是Patient class 的字段。这就是您收到此错误的原因。

从代码中,您可能想要设置parseArrivalDatedepartureDate变量,如下所示。

defaults=dict(
    date_arrival=parseArrivalDate,
    date_departure=departureDate,
),

这是修复后的完整代码,

from datetime import datetime

arrivalDate = response.json()['bookings'][0]['arrival']['date']
print(arrivalDate)
parseArrivalDate = datetime.strptime(arrivalDate, '%Y-%m-%d')
print(type(parseArrivalDate))

departureDate = response.json()['bookings'][0]['departure']['date']

patientIdWebFlow = response.json()['patient']['patient_id']

patientsIdDB = Patient.objects.values('coreapi_id')

# here I loop through the id's and check if the record from API match  with some of the records in my database and tried to save the date for this id.
for patientIdDB in patientsIdDB:
    if patientIdWebFlow == int(patientIdDB['coreapi_id']):
        print('they matched')
        datesPatients, created = Patient.objects.get_or_create(
            coreapi_id=int(patientIdDB['coreapi_id']),
            defaults=dict(
                date_arrival=parseArrivalDate,
                date_departure=departureDate,
            ),
        )
    else:
        print('they not matched')

一个更正确的实现不会循环遍历数据库中的每个 Patient 只是为了准确地找到一个 Patient 是

from datetime import datetime

json = response.json()
booking = json['bookings'][0]

arrivalDate = booking['arrival']['date']
departureDate = booking['departure']['date']
patientIdWebFlow = json['patient']['patient_id']
parseArrivalDate = datetime.strptime(arrivalDate, '%Y-%m-%d')

did_update = Patient.objects.filter(coreapi_id=patientIdWebFlow).update(
    date_arrival=parseArrivalDate,
    date_departure=departureDate,
)
if not did_update:
    raise ValueError("No records got updated.")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM