Base Field

Table of contents

  1. Example Usage
  2. Allowed Attributes
    1. Default
    2. Required
    3. Column Name
    4. Validator

All fields are extend from base Field This field can be used to store any kind of data in Firestore. This work also like Dynamic Field

Example Usage

class User(Model):
    name = Field()
    age = Field()


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

Allowed Attributes

The following attributes supported by DateTime Field.

  1. default
  2. required
  3. column_name
  4. validator
  • Default

    Default value for field. This is base attribute that is available in all fields. Set default value for field if no value is provided

Example Usage

class User(Model):
    name = Field(default="Azeem")


u = User()
u.save()

print(u.name)  # Azeem
  • Required

    Set True if value is required for the field. If no value is provided error raise. This is base attribute that is available in all fields

Example Usage

class User(Model):
    name = Field(required=True)


u = User()
u.name = "Azeem"
u.save()
  • Column Name

Set different column name in Firestore instead of field name. By default column name is same as the field name but you can change the column name in Firestore using this attribute. This is base attribute that is available in all fields

Example Usage

class User(Model):
    name = Field(column_name="full_name")


u = User()
u.name = "Azeem"
u.save()
  • Validator

Validate given value of field. This is base attribute that is available in all fields

Example Usage

def check_email(field_val):
    if '@' in field_val:
        return True
    else:
        return False


class User(Model):
    email = Field(validator=check_email)


u = User()
u.email = 'dev@octabyte.io'
u.save()

If field not passed the validation then an error will raise. You can also define the custom error message

def check_email(field_val):
    if '@' in field_val:
        return True
    else:
        return (False, 'Email must contain @ sign')

You can also pass in a dict for custom kwargs as validator_kwargs when defining the field. You’ll also need to make the validator function takes in **kwargs as an argument. This is useful for passing extra arguments or metadata to validator functions.

Example Usage

def check_email(field_val, **kwargs):
    if kwargs.get("key"):
        # some logic

    if '@' in field_val:
        return True
    else:
        return False


class User(Model):
    email = Field(validator=check_email, validator_kwargs={"key": "val"})

    # If you want to programmatically add kwargs after object instantiation
    def set_kwargs(self, kwargs)
        modified_field = Field(validator=check_email, validator_kwargs=kwargs)
        modified_field.contribute_to_model(User, "email")


u = User()
u.email = 'dev@octabyte.io'
u.save()

# Setting kwargs after object instantiation
u = User()
new_kwargs = {"key": "val_2"}
u.set_kwargs(new_kwargs)
u.save()

Copyright © 2019 FireO All rights reserved. Powered by OctaByte