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第三方库


热门话题
java GridBagLayout不填充区域   java Memozied Fibonacci未运行与常规Fibonacci解决方案   Java Web启动未启动问题   Java中异常和if-then的区别   java从命令提示符运行批处理文件获取错误   socket在Java中验证SSL证书的公共名称   如何在JAVA中检查字符串数组中的相等字   用java语言将音频文件转换成文本文件的语音识别   java为什么foo(1,2,3)没有传递给varargs方法foo(对象…)作为整数[]   java通过蓝牙将奇怪的数据从Arduino传输到Android   java ContainerRequestFilter获取空entitystream   java如何从安卓 studio中删除不兼容类型错误   基本Java错误   在Spring引导中使用REST API时发生java错误   javascript通过从SQL查询派生的URL打开页面