ORM¶
New in version 1.0.0.
Warning
This feature is experimental. Feel free to submit suggestions or feedback in our Github repo
Model¶
The Model
class allows you create an orm-style class for your
Airtable tables.
>>> from pyairtable.orm import Model, fields
>>> class Contact(Model):
... first_name = fields.TextField("First Name")
... last_name = fields.TextField("Last Name")
... email = fields.EmailField("Email")
... is_registered = fields.CheckboxField("Registered")
... partner = fields.LinkField("Partner", "Contact", lazy=False)
...
... class Meta:
... base_id = "appaPqizdsNHDvlEm"
... table_name = "Contact"
... api_key = "keyapikey"
Once you have a class, you can create new objects to represent your
Airtable records. Call save()
to create a new record.
>>> contact = Contact(
... first_name="Mike",
... last_name="McDonalds",
... email="mike@mcd.com",
... is_registered=False
... )
...
>>> assert contact.id is None
>>> contact.exists()
False
>>> assert contact.save()
>>> contact.exists()
True
>>> contact.id
rec123asa23
You can read and modify attributes. If record already exists,
save()
will update the record:
>>> assert contact.is_registered is False
>>> contact.save()
>>> assert contact.is_registered is True
>>> contact.to_record()
{
"id": recS6qSLw0OCA6Xul",
"createdTime": "2021-07-14T06:42:37.000Z",
"fields": {
"First Name": "Mike",
"Last Name": "McDonalds",
"Email": "mike@mcd.com",
"Resgistered": True
}
}
Finally, you can use delete()
to delete the record:
>>> contact.delete()
True
- class pyairtable.orm.model.Model(**fields)[source]¶
This class allows you create an orm-style class for your Airtable tables.
This is a meta class and can only be used to define sub-classes.
The
Meta
is reuired and must specify all three attributes:base_id
,table_id
, andapi_key
.>>> from pyairtable.orm import Model, fields >>> class Contact(Model): ... first_name = fields.TextField("First Name") ... age = fields.IntegerField("Age") ... ... class Meta: ... base_id = "appaPqizdsNHDvlEm" ... table_name = "Contact" ... api_key = "keyapikey" ... timeout: Optional[Tuple[int, int]] = (5, 5) ... typecast: bool = True
- classmethod all(**kwargs)[source]¶
Returns all records for this model. See
all()
- Return type
List
[~T]
- created_time: str = ''¶
- exists()[source]¶
Returns boolean indicating if instance exists (has ‘id’ attribute)
- Return type
bool
- classmethod first(**kwargs)[source]¶
Returns first record for this model. See
first()
- Return type
List
[~T]
- classmethod from_id(record_id, fetch=True)[source]¶
Create an instance from a record_id
- Parameters
record_id (
str
) – An Airtable record id.
- Keyward Args:
- fetch: If True, record will be fetched and fields will be
updated. If False, a new instance is created with the provided id, but field values are unset. Default is True.
- Returns
Instance of model
- Return type
(
Model
)
- id: str = ''¶
Fields¶
Field are used to define the Airtable column type for your pyAirtable models.
Internally these are implemented as descriptors, this allows us to proxy getting and settings values, while also providing a type-annotated interface.
>>> from pyairtable.orm import Model, fields
>>> class Contact(Model):
... name = fields.TextField("Name")
... is_registered = fields.CheckboxField("Registered")
...
... class Meta:
... ...
>>> contact = Contact(name="George", is_registered=True)
>>> assert contact.name == "George"
>>> reveal_type(contact.name) # -> str
>>> contact.to_record()
{
"id": recS6qSLw0OCA6Xul",
"createdTime": "2021-07-14T06:42:37.000Z",
"fields": {
"Name": "George",
"Registered": True,
}
}
Link Fields¶
In addition to standard data type fields, the LinkField
class
offers a special behaviour that can fetch linked records.
In other words, you can transverse related records through their Link Fields
:
>>> from pyairtable.orm import Model, fields
>>> class Company(Model):
... name = fields.TextField("Name")
... class Meta:
... ...
...
>>> class Person(Model):
... company = fields.LinkField("Company", Company, lazy=False)
... class Meta:
... ...
...
>>> contact.from_id("recS6qSLw0OCA6Xul")
>>> contact.company.name # outputs value of Company.name attribute
- class pyairtable.orm.fields.CheckboxField(field_name, validate_type=True)[source]¶
Airtable Checkbox field. Uses
bool
to store value
- class pyairtable.orm.fields.DateField(field_name, validate_type=True)[source]¶
Airtable Date field. Uses
Date
to store value
- class pyairtable.orm.fields.DatetimeField(field_name, validate_type=True)[source]¶
Airtable Datetime field. Uses
datetime
to store value
- class pyairtable.orm.fields.EmailField(field_name, validate_type=True)[source]¶
Airtable Email field. Uses
str
to store value
- class pyairtable.orm.fields.FloatField(field_name, validate_type=True)[source]¶
Airtable Number field with Decimal precision. Uses
float
to store value
- class pyairtable.orm.fields.IntegerField(field_name, validate_type=True)[source]¶
Airtable Number field with Integer Precision. Uses
int
to store value
- class pyairtable.orm.fields.LinkField(field_name, model, lazy=True)[source]¶
Airtable Link field. Uses
List[Model]
to store value- __init__(field_name, model, lazy=True)[source]¶
- Parameters
field_name (
str
) – Name of Airtable Columnmodel (
Union
[str
,Type
[~T_Linked]]) – Model of Linked Type. Must be subtype ofModel
lazy – Use True to load linked model when looking up attribute. False will create empty object with only id but will not fetch fields.
- Usage:
>>> TODO