pyairtable icon indicating copy to clipboard operation
pyairtable copied to clipboard

Enhance `Model.save` Method to Allow Field-specific Updates

Open BAPCon opened this issue 1 year ago • 2 comments

Introduces enhancements requested in #378 to the Model.save method, allowing specific field updates to a record rather than the entire record.


contact = Contact(
    first_name="Benjamin",
    last_name="Perkins"
)

# Specify fields to save
contact.save(fields=['first_name'])

Contact.from_id(contact.id)._fields
>>> {'First Name': 'Benjamin'}

ORM attribute name vs Airtable field name

I think the fields should be specified via their ORM attribute name: first_name, last_name. This would maintain consistency and be more Pythonic.

BAPCon avatar Aug 15 '24 01:08 BAPCon

I understand what you're going for here, but I would still prefer the approach of tracking changes to fields behind-the-scenes (so that callers don't need to keep track of which fields they're modifying). I can't think of any other use cases where you'd want to avoid saving field values that were changed on the model, and adding this kwarg to the API will create debt that we'll need to support indefinitely.

If you really need this behavior today, you can do something like:

update_fields = orm_obj.to_record(only_writable=True)["fields"]
update_fields = {k: v for (k, v) in update_fields.items() if k in SAVE_FIELDS}
orm_obj.meta.table.update(orm_obj.id, update_fields)

mesozoic avatar Aug 16 '24 01:08 mesozoic

I've posted #381 as an alternate proposal to address the issue. Open to questions or comments there.

mesozoic avatar Aug 21 '24 03:08 mesozoic