【发布时间】:2022-01-01 10:51:15
【问题描述】:
我正在尝试将数据推送到 Firebase,我能够遍历一个数组并在每个循环中推送信息。 但我需要在其中添加一些图片(所以这就像在字典定义中循环一个数组)。我有一个数组中的所有链接。 这是我目前的代码。
def image_upload():
for i in range(len(excel.name)):
doc_ref = db.collection('plans').document('doc-name').collection('collection-name').document()
doc_id = doc_ref.id
data = {
'bedroomLocation': excel.bedroomLocation[i],
'bedrooms': excel.bedrooms[i],
'brand': excel.brand[i],
'catalog': excel.catalog[i],
'category': excel.category[i],
'code': excel.code[i],
'depth': excel.depth[i],
'description': excel.description[i],
'fullBaths': excel.fullBaths[i],
'garage': excel.garage[i],
'garageBays': excel.garageBays[i],
'garageLocation': excel.garageLocation[i],
'garageType': excel.garageType[i],
'date': firestore.SERVER_TIMESTAMP,
'id': doc_id,
'halfBaths': excel.halfBaths[i],
'laundryLocation': excel.laundryLocation[i],
'name': excel.name[i],
'onCreated': excel.onCreated[i],
'productType': excel.productType[i],
'region': excel.region[i],
'sqf': excel.sqf[i],
'state': excel.state[i],
'stories': excel.stories[i],
'tags': [excel.tags[i]],
'width': excel.width[i],
}
doc_ref.set(data)
这很好,但我真的不知道如何遍历链接数组。 这是我在上面复制的块下面尝试的。
for j in range(len(excel.gallery)):
if len(excel.gallery[j]) != 0:
for k in range(len(excel.gallery[j])):
data['gallery'] = firestore.ArrayUnion(
[{'name': excel.gallery[j][k][0], 'type': excel.gallery[j][k][1],
'url': excel.gallery[j][k][2]}])
print(data)
doc_ref.set(data)
len(excel.gallery) 与 len(excel.name) 的长度相同 但是,每个 j 位置都有不同数量的链接。 如果我在数据定义中声明图库并使用 ArrayUnion 并预定义多个信息,它可以正常工作,但我需要使用该数组将信息推送到 Firebase。
【问题讨论】:
标签: python arrays firebase google-cloud-firestore