NoSQL对象模型数据库

firebase-orm的Python项目详细描述


FirebaseormPython

NoSQL数据库FireStore的Django类模型。


安装

$ pip install firebase_orm

初始化

在项目的根目录中创建settings.py:

settings.py

CERTIFICATE='path/to/serviceAccountKey.json'BUCKET_NAME='<BUCKET_NAME>.appspot.com'
证书
创建Firebase console项目并下载带有服务帐户凭据的json文件后。
桶名
存储桶名称不能包含gs://或任何其他协议前缀。例如,如果Firebase console中显示的bucket url是gs://bucket-name.appspot.com,则传递字符串bucket name.appspot.com

用法

创建模型:

fromfirebase_ormimportmodelsclassArticle(models.Model):headline=models.TextField()type_article=models.TextField(db_column='type')classMeta:db_table='medications'def__str__(self):returnself.headline

使用API:

创建对象

为了在python对象中表示云firestore数据,firebaseorm使用了一个直观的系统: 模型类表示集合, 类的实例表示集合中的文档。

若要创建对象,请使用模型类的关键字参数将其实例化, 然后调用save()将其保存到数据库中。

# Import the models we created
>>> frommodelsimportArticle# Create a new Article.
>>> a=Article(headline='Django is cool')# Save the object into the database. You have to call save() explicitly.
>>> a.save()

检索所有对象

从集合中检索文档的最简单方法是获取所有文档。 为此,请在管理器上使用all()方法:

>>> all_Article=Article.objects.all()

方法返回数据库中所有集合的列表实例项目。

# Now it has an ID.
>>> a.id1

# Fields are represented as attributes on the Python object.
>>> a.headline'Django is cool'

保存对对象的更改

要保存对数据库中已有对象的更改,请使用save()。

给定一个已经保存到数据库的项目实例A, 此示例更改其名称并更新其在数据库中的记录:

>>> a.headline='FirebaseORM is cool'>>> a.save()

这将在幕后执行document.update()方法。 在显式调用save()之前,firebaseorm不会命中数据库。

# Firebase ORM provides a rich database lookup API.
>>> Article.objects.get(id=1)<Article: FirebaseORM is cool>
>>> Article.objects.get(id=2)Traceback (most recent call last):
...DoesNotExist: Article matching query does not exist.

字段选项:

以下参数可用于所有字段类型。所有都是可选的。

field.db_列

If contains characters that aren’t allowed in Python variable names – use db_column. The name of the firestore key in document to use for this field. If this isn’t given, FirebaseORM will use the field’s name.

字段类型:

自动场

class autofield()

By default, FirebaseORM gives each model the following field:

id=models.AutoField(primary_key=True)

文本字段

class文本字段(**选项)

Text string Up to 1,048,487 bytes (1 MiB - 89 bytes). Only the first 1,500 bytes of the UTF-8 representation are considered by queries.

TextField has not extra required argument.

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
与ReentrantLock相比,java ReentrantReadWriteLock的性能非常差   java如何使用Maven Android Studio正确导入?   安卓将ADB添加到我的Java PC应用程序   反射Java getDeclaredConstructor失败,来自JUnit的NoSuchMethodException   JSP上siteedit标记库的java替代   JavaSpring环境概要文件和JPA   java中是否有一个类似于StringBuilder的类,唯一的区别是它具有固定的长度?   JavaMathContext。小数点32 vs MathContext。小数点64,使用哪一个,为什么?   java使用spring在Ibm Websphere MQ中实现重试逻辑   java调用SpriteBatch。开始()和结束()   java有一种从文本中读取文本的方法。文件,并将其设置为pom中的maven属性。xml专家?   java让sitemesh使用struts2   Java Swing:在现有窗口上定位对话框   使用带有MemSql的JPA本机查询的java Select json列