Reference Field

Table of contents

  1. Example Usage
  2. Allowed Attributes
    1. Default
    2. Required
    3. Column Name
    4. Validator
    5. Auto Load
    6. Example Usage
    7. On Load
    8. Example Usage

A DocumentReference refers to a document location in a Firestore database and can be used to write, read, or listen to the location. The document at the referenced location may or may not exist.

Example Usage

class Company(Model):
    name = TextField()


class Employee(Model):
    name = TextField()
    company = ReferenceField(Company)

c = Company(name="Abc_company")
c.save()

e = Employee()
e.name = 'Employee Name'
e.company = c
e.save()

Allowed Attributes

The following attributes supported by Reference Field.

  1. default
  2. required
  3. column_name
  4. validator
  5. auto_load
  6. on_load
  • 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

  • Column Name

    Set different column name in Firestore instead of field name. This is base attribute that is available in all fields. Read More

  • Validator

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

  • Auto Load

    Load reference document automatically, by default it is True If you disable the auto_load then you can get document by get() method.

Example Usage

class Employee(Model):
    name = TextField()
    company = ReferenceField(Company, auto_load=False)


e = Employee.collection.get(emp_key)
print(e.company)  # object of ReferenceDocLoader

# Reference document can be get using get() method
com = e.company.get()
print(com.name)
  • On Load

    Call user specify method when reference document load

Example Usage

class Employee(Model):
    name = TextField()
    company = ReferenceField(Company, on_load="do_something")

    def do_something(self, company):
        # do something with company document
        print(company.name)