IDField

Table of contents

  1. Example Usage
  2. Allowed Attributes
    1. Default
    2. Required
    3. Include in document (v2.0.0)

IDField is special field which is used to specify custom id for documents. If no id specify then id will be generated automatically.

Example Usage

from fireo.models import Model
from fireo.fields import IDField

class User(Model):
    user_id = IDField()



u = User()
u.user_id = "custom_doc_id"
u.save()
# After save id will be saved in `user_id`
print(self.user_id)  # custom_doc_id

Allowed Attributes

The following attributes supported by ID Field.

  1. default
  2. required
  3. include_in_document
  • Default

    Default value for field. This is base attribute that is available in all fields. Read More

  • Required

    Set True if value is required for the field. This is base attribute that is available in all fields. Read More

  • Include in document (v2.0.0)

    Set True if you want to include the ID as a field in document. It can be useful if you need to do filters that are not allowed with the document ID in the Firestore. Example:

    from fireo.models import Model
    from fireo.fields import IDField, TextField
      
    class User(Model):
        id = IDField()
        name = TextField()
      
    user = User(name='John')
    user.save()
      
    # Filter by ID prefix
    # Note: the filtering is done on the document field "id" and not on the document ID.
    id_prefix = 'my-prefix'
    query = User.collection.filter('id', ">=", id_prefix).filter('id', "<", id_prefix + "\ufffd")
    

    You still can do filtering by document ID using the FieldPath.document_id():

    from google.cloud.firestore_v1.field_path import FieldPath
      
    query = User.collection.filter(FieldPath.document_id(), 'in',  ['some-id', 'some-id-2']) 
    

Copyright © 2019 FireO All rights reserved. Powered by OctaByte