gtalarico / pyairtable

Python Api Client for Airtable
https://pyairtable.readthedocs.io
MIT License
765 stars 138 forks source link

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

Closed BAPCon closed 2 weeks ago

BAPCon commented 1 month ago

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.

mesozoic commented 1 month ago

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 commented 3 weeks ago

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