Reference Field
Table of contents
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.
-
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 theauto_load
then you can get document byget()
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)
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)