Meta Class

Table of contents

  1. Abstract Model
  2. Collection Name
  3. Missing Fields
  4. Lower case
  5. Ignore None Field (v1.3.5)
  6. Collection Name Generator (v2.0.0)
  7. Columns Name Generator (v2.0.0)
  8. Default Manager class (v2.0.0)

Meta class is used for Model configuration. Here are some common configuration for Model

Abstract Model

Abstract model is used to create some common fields into a number of other models.

Note: Abstract model does not support IDField

Exmaple Usage

class Animal(Model):
    move = TextField()
    eat = TextField()

    class Meta:
        abstract = True


class Bird(Animal):
    name = TextField()


class Fish(Animal):
    name = TextField()


b = Bird()
b.name = 'Sparrow'
b.move = 'Move by Flying'
b.eat = 'Eats bird food'
b.save()


f = Fish()
f.name = 'fish'
f.move = 'Move by swimming'
f.eat = 'Eats sea food'
f.save()

Collection Name

Set collection name in Firestore if no collection name specify then by default Model name will be used for collection, For example: UserProfile will become user_profile

Example Usage

class User(Model):
    name = TextField()

    class Meta:
        collection_name = "my_user_collection"

Missing Fields

Manage how fields behave when they are not in Model but in Firestore, this is happen when you change model after adding some records. By default missing field merge with the model. Possible values are merge, ignore, raise_error

Example Usage

class User(Model):
    name = TExtField()
    age = NumberField()

    class Meta:
        missing_field = 'ignore'

Lower case

Firestore is case sensitive if you save name as Azeem you can’t filter it like azeem So it is best practice to save data in lower case it help you to search easily. FirO allow to save data in lower case and search data without case sensitive.

Example Usage

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

    class Meta:
        to_lowercase = True

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


# Filter result All three are works and give same result
User.collection.filter('name', '==', 'azeem').get()
User.collection.filter('name', '==', 'Azeem').get()
User.collection.filter('name', '==', 'AzEEm').get()

Ignore None Field (v1.3.5)

By default None fields are ignore in Firestore. You can change setting in Meta class by defining option ingore_none_field possible values are True or False. Default value is True

Example Usage

class User(Model):
    name = TextField()
    address = TextField()

    class Meta:
        ignore_none_field = False

user = User()
user.name = "Azeem"
user.save()

# If you check Firestore you will see null value in front of address field
# To ignore null values in Firestore set ignore_none_field = True

Collection Name Generator (v2.0.0)

By default collection name is generated as snake case of model name. You can change this behavior by defining collection_name_generator in Meta class.

Example Usage

class User(Model):
    name = TextField()
    address = TextField()

    class Meta:
        collection_name_generator = lambda model_name: model_name.lower() + 's'

assert User.collection_name == 'users'

Columns Name Generator (v2.0.0)

By default field name is used as column name in Firestore. You can change this behavior by defining columns_name_generator in Meta class.

Example Usage

class User(Model):
    name = TextField()
    # Explicitly defined column name will not be affected by column_name_generator
    address = TextField(column_name='address_line_1')

    class Meta:
        column_name_generator = lambda field_name: camel_case(field_name)

Default Manager class (v2.0.0)

By default Model use Manager class to manage data in Firestore. You can change this behavior by defining default_manager_class in Meta class.

Example Usage

class User(Model):
    name = TextField()
    address = TextField()

    class Meta:
        default_manager_class = CustomManager

Copyright © 2019 FireO All rights reserved. Powered by OctaByte