Link Search Menu Expand Document

Adding Data

Table of contents

  1. Adding Data
  2. By creating model object
    1. Example Usage
    2. Using Constructor
      1. Example Usage
    3. Using From Dict
    4. Custom id
  3. Upsert (Merge fields)
    1. Example Usage
  4. Using Manager
    1. Example Usage
  5. Sub collection
    1. Example Usage

Adding Data

There are two ways to add data in Firestore.

  1. By creating model object
  2. Using Manager

By creating model object

Create model object and add values to it and save() the model. After saving model model id and model key is attached with model object.

Example Usage

from fireo import models as mdl

class User(mdl.Model):
    name = mdl.TextField()
    age = mdl.NumberField()

u = User() = "Azeem"
u.age = 26

print(  # xaIkLAGEjkSON
print(u.key)  # user/xaIkLAGEjkSON 

Using Constructor

Values can also be passed in constructor.

Example Usage

u = User(name="Azeem", age=26)

print(  # xaIkLAGEjkSON
print(u.key)  # user/xaIkLAGEjkSON

key contain more information e.g id, parent collection, parent document these things can be get from key using utils class.and useful when getting, updating or delete data. key also used for creating sub collections.

Using From Dict

Model can also create from dict

Example Usage

model_dict = {'name': 'Azeem', 'age': 26}
u = User.from_dict(model_dict)

print(  # xaIkLAGEjkSON
print(u.key)  # user/xaIkLAGEjkSON

Custom id

Custom id can also be specified by using IDField

Example Usage

from fireo.models import Model
from fireo.fields import IDField, TextField, NumberField

class User(Model):
    user_id = IDField()
    name = TextField()
    age = NumberField()

u = User()
u.user_id = 'custom_doc_id' = "Azeem"
u.age = 26

print(u.user_id)  # custom_doc_id

If you add IDField and not specify any id then id will be stored in this field.


u = User() = "Azeem"
u.age = 26

print(u.user_id)  # xaIkLAGEjkSON

# will be None in this case
print(  # None

You can choose any name for id field it can be id itself

Upsert (Merge fields)

If the document does not exist, it will be created. If the document does exist, its data should be merged into the existing document, as follows

Example Usage

u = User() = "custom-id" = "Azeem"
# OR

# Both upsert() and save(merge=True) is same thing

If you’re not sure whether the document exists, pass the option to merge the new data with any existing document to avoid overwriting entire documents.

Using Manager

Data can be saved by using manger without create model object. Manager return model object after saving data. collection is default manager for models.

Example Usage

u = User.collection.create(name="Azeem", age=26)


Sub collection

A subcollection is a collection associated with a specific document. In FireO world you can save one model inside another model parent child relation.

Example Usage

class Post(Model):
    title = TextField()
    content = TextField()

class Review(Model):
    name = TextFile()
    message = TextFile()

p = Post(title="My First Post", content="Post content")

r = Review(parent=p.key) = "Azeem"
r.message = "Nice post"