fopina / django-bulk-update-or-create

`bulk_update_or_create` for Django model managers
MIT License
148 stars 16 forks source link

Add option for update_fields to choose 'all' #35

Open arady22 opened 2 years ago

arady22 commented 2 years ago

Title when calling the function to update all the fields instead of calling

model_field_names = [field.name for field in model._meta.get_fields()]
model.objects.bulk_update_or_create(bulk_data, model_field_names, match_field='object_guid')

we call model.objects.bulk_update_or_create(bulk_data, 'all', match_field='object_guid')

fopina commented 2 years ago

Curious to understand why would you update all fields (apart from developer laziness).

This module is meant to bulk update (as in update many records). If you have to specify the fields that you're changing when modifying the objects, why would you NOT specify the fields in the update and incur in bigger data transfer than required?

arady22 commented 2 years ago

Long story short we update/sync records coming from JSON API so we have no idea which records and fields really changed so we choose to update all.

btw we have to exclude primary keys from the model_field_names.

fopina commented 1 year ago

The main reason I don't think it's good to support all fields is to avoid mistakes.

Imagine you have a model that fully maps to a JSON you consume. So you use all.

At some point, you add a new field to the model to be manually managed, it's not part of the parsed JSON.

If you don't remember to replace "all" with the list of all fields except that one, this will clear that field (because the objects from the JSON file always leave it empty).

I'll leave this issue open to document a workaround to pass all fields (ie: Model.get_fields()) but leaving it up to consumer to abuse that.

housUnus commented 1 year ago

The *all options is helpful in case we have a model with many fields, it's tidy work to mention all the fields of the model, when it should select all and exclude the match_field, which BTW it'll be better if it is specified as a list 'cause when I receive the object without an id I would like to compare the title + another field to ensure the uniqueness then update.