Closed gildastone closed 1 week ago
Totally agree – this would be a very useful behavior (and should probably be the default behavior, rather than something implementers would have to explicitly request with a flag or a separate method call).
One way I can think of approaching this is to keep track of which values have been modified in memory since the object was created. Then it would be possible to only send those fields to the table.update
method. There are a few good pieces of prior art for SQLAlchemy, Django, and Rails.
Pull requests welcome! 😁
@mesozoic Working on this now, pull request probably today.
@BAPCon great to hear you're working on it. Let me know if you need some help. @mesozoic great suggestion to leverage existing art from those projects.
@gildastone I addressed the original issue and the move to being able to specify which fields to update, I believe my pull request accomplishes what the user stated. In general @gildastone I would suggest also using Model.batch_save
when possible.
I've done some testing with my changes to compare specifying fields to update vs all fields at the same time. The runtime savings vary from 0% - 40% when specifying the fields, not sure exactly why such a large variation but the savings seem inconsistent. It seems dependent on whether you are using Model.batch_save
or Model.save
and the total number of records.
If anyone wants to review my changes or test the execution times yourself, I think that may be a good idea before implementing the larger concept of a field's state being dirty that @mesozoic mentioned.
@BAPCon Understood. Thanks. Just added my review to https://github.com/gtalarico/pyairtable/pull/379.
Is there a way to save specific fields of a Model and not the entire record while using the
Model.save
method?Indeed, I have a record that has a pretty heavy payload (number of fields). I'm fetching the record, doing some operations/updates on it on specific fields and then save again.
I would like to only save/update those specific fields instead of posting the whole record (see pyairtable.orm.model.Model.save()):
I couldn't find a way with the current ORM. Is there a solution?
If not, we could update the
pyairtable.orm.model.Model.save
method to (adding thefields_to_save: List = []
argument):