Skip to content

Commit

Permalink
Fix field reference names to Pythonic
Browse files Browse the repository at this point in the history
To help maintain consistency and provide a more Pythonic experience, field refences passed to `Model.save()` should be Pythonic and by their attribute name in the model class.
  • Loading branch information
BAPCon committed Aug 15, 2024
1 parent 042ab36 commit e518a55
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions pyairtable/orm/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,18 @@ def save(self, fields: Optional[List[str]] = None) -> bool:
"""
if self._deleted:
raise RuntimeError(f"{self.id} was deleted")

table = self.meta.table
attribute_map = self._attribute_descriptor_map()

if fields:
field_map = self._field_name_descriptor_map()
# Only include specified fields in the payload
# Convert ORM attribute names to Airtable field names
update_fields = {
field: value for field, value in self._fields.items() if field in fields and field not in field_map[field].readonly
attribute_map[field].field_name: None if self._fields.get(attribute_map[field].field_name) is None else attribute_map[field].to_record_value(self._fields.get(attribute_map[field].field_name))
for field in fields
if field in attribute_map and not attribute_map[field].readonly
}
else:
# Include all fields except read-only ones
update_fields = self.to_record(only_writable=True)["fields"]

if not self.id:
Expand All @@ -253,6 +256,7 @@ def save(self, fields: Optional[List[str]] = None) -> bool:

self.id = record["id"]
self.created_time = datetime_from_iso_str(record["createdTime"])

return did_create

def delete(self) -> bool:
Expand Down

0 comments on commit e518a55

Please sign in to comment.