python - 如何在firebase(python)上推送多个新对象
问题描述
我想将数据从 json 推送到 firebase,而不为每个对象生成新密钥。理想情况下,我想定义密钥的名称。
这是我的代码:
import pyrebase
import json
config = {my firebase config}
firebase = pyrebase.initialize_app(config)
db = firebase.database()
with open('myjson.json') as response:
data = json.load(response)
db.child("customer").push(data)
我的 json 看起来像这样:
{
"first_customer": {
"createdat": "2019-08-09T06:40:10+0000",
"expires": 1572039000.0,
},
"second_customer": {
"createdat": "2019-08-09T06:33:39+0000",
"expires": 1570048200.0,
}
}
但实际上我的代码在 firebase 上给了我这个:
"customer": {
"-Lm_M6OM7MCV3_PFvcSH(random_id_given_by_firebase)": {
"first_customer": {
"createdat": "2019-08-09T06:40:10+0000",
"expires": 1572039000.0,
},
"second_customer": {
"createdat": "2019-08-09T06:33:39+0000",
"expires": 1570048200.0,
}
}
}
我期望的是:
"customer": {
"first_customer": {
"createdat": "2019-08-09T06:40:10+0000",
"expires": 1572039000.0,
},
"second_customer": {
"createdat": "2019-08-09T06:33:39+0000",
"expires": 1570048200.0,
}
}
解决方案
要控制完整路径,请使用set
. 所以像:
db.child("customer").child("keyforcustomer").set(data)
推荐阅读
- javascript - 未注册的 QueryParams 在 $state.transitionTo 上被剥离
- spring-data-jpa - 如何从 application.property 文件访问 url 中的变量
- c++ - 自 1803 年以来,带有 GWLP_WNDPROC 的 SetWindowLongPtr 不适用于全屏应用程序(C++)
- c - C 数据类型的范围
- javascript - 如何验证电子邮件和最小长度验证
- python - 在确定具有第一优先级的列表后,通过选择具有最小值的列表来过滤嵌套列表?
- algorithm - 具有矛盾约束的项目的最优排序
- xslt - XSLT 按顺序选择元素
- rust - Rust 中通配符/前奏导入的最佳实践是什么?
- ionic-framework - 离子框架改变颜色导航栏