简体   繁体   English

如何将对象传递给 Python 中的 Google Datastore 函数?

[英]How do I pass an object to a Google Datastore function in python?

I have the following code, I want to pass a museum object with attributes name and location to the create_booking function?我有以下代码,我想将具有属性名称和位置的博物馆对象传递给 create_booking 函数? How can I do this?我怎样才能做到这一点?

I tried things like create_booking(user, {name, location}, date, arHr, leHr)我尝试了诸如 create_booking(user, {name, location}, date, arHr, leHr) 之类的东西

@staticmethod
    def create_booking(user, museum, date, arHr, leHr):
        task = datastore.Entity(DB.client.key("Bookings"))
        if len(DB.get_museum(museum.name)) == 0:
            return "Museum not found", 404
        if DB.get_bookings_number_by_date(museum.name, date)[0] > 10:
            return "To many bookings for this date", 401
        task.update(
            {
                "user": user,
                "museum": {
                    "name": name,
                    "location": location
                },

                "date": date,
                "arriving hour": arHr,
                "leaving hour": leHr
            }
        )
        DB.client.put(task)

From your code, it seems museum is not a dictionary (cos you're doing museum.name ).从你的代码来看, museum似乎不是字典(因为你在做的是museum.name )。 This means you can't pass it as you have done in your example.这意味着您不能像在示例中那样通过它。 You'll have to instantiate it, add the values for the attributes and pass it ie something like (assuming Museum is a KIND)您必须实例化它,为属性添加值并传递它,例如(假设博物馆是一种)

museum = Museum (name=<name>, location=<location>)
create_booking(user, museum, date, arHr, leHr)

You can use firestore for your project, which is datastore in google cloud.您可以将 firestore 用于您的项目,即谷歌云中的数据存储。 If you already have a Google cloud project, you can use the same project for firestore as well.如果您已经有一个 Google Cloud 项目,您也可以将相同的项目用于 firestore。 Please follow the following tutorial about how to create service account and generate key.请按照以下有关如何创建服务帐户和生成密钥的教程进行操作。

Please check the below code sample about how to create the collections, documents and update the fields.请查看以下代码示例,了解如何创建集合、文档和更新字段。

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore



# initializations 
cred = credentials.Certificate('XXXXXXXXXXXXXXXXX.json')
firebase_admin.initialize_app(cred)
db = firestore.client()



#adding first data
doc_ref = db.collection('Museum_booking').document('cust1')

doc_ref.set({

    'name':'Steve',
    'lname':'Smith',
    'location':'Florida',
    'age':24


})



#adding second data
doc_ref = db.collection('Museum_booking').document('cust2')
doc_ref.set({

    'name':'John',
    'lname':'Doe',
    'email':'john@gmail.com',
    'location':'Texas',
    'age':26


})

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

相关问题 如何将本地Google App Engine Python数据存储区复制到本地Google App Engine Java数据存储区? - How do I copy local Google App Engine Python datastore to local Google App Engine Java datastore? 如何在python中的函数中传递对象的实例作为参数? - How do I pass instance of an object as an argument in a function in python? 如何在Python中将数组传递给函数? - How do I pass an array to a function in Python? 如何在Python Google Appengine数据存储区中实现一对一关系? - How to do one to one relation in python google appengine datastore? 如何在 Class 中使用 ZA7F5F354316B9272117392 中的调用方方法将 Object 传递给 Function? - How Do I Pass an Object to a Function Within a Class Using a Caller Method in Python? 如何将大量数据插入谷歌数据存储? - how do i insert large amounts of data to google datastore? 如何向 google 数据存储 StringListProperty 添加选项? - How do I add choices to google datastore StringListProperty? 如何根据关键字将过滤器应用于Google数据存储区集合? - How do I apply a filter to a google datastore collection based on a key? 如何在 Google Cloud Platform 的 DataStore 中存储值? - How do I store values in DataStore in Google Cloud Platform? 如何将对象传递给python中的函数 - how to pass object to a function in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM