I've already created a ticket that ended up with 'WONTFIX' here 
<https://code.djangoproject.com/ticket/33770>.
Please read it first.

The scenario that led me trying to add that feature:
Well, I'v a model that has a primary key and unique field...
Every  X period of time I've a cron that calls an external API to load 
updated data and store it in my Django project db.
Thing is I want to execute the update query without having to have the pk 
with me.. I already have the unique field in hand (got it from the API) and 
it should be enough to identify what object to be updated..

example code that will do so..

*# call external API and load data as list[dict]*
*objs = list()*
*for record in data:*
*    # Just create instance of MyModel class with values needed..*
*    objs.append(MyModel(my_unique_field=data['national_id'], name="New 
Name"))*

*# I expect to do:MyModel.objects.bulk_update(objs, fields=["name"], 
unique_field="my_unique_field")*

The current implementation requires every object to have its primary key 
set, but, that's not always the case. Also, if the primary key is 
my-django-project-generated, there's no way AFAIK to get those PKs without 
having to query them from DB, which will be a very expensive query to run 
if the data is large.

You can find the POC PR here: https://github.com/django/django/pull/15764

I really thing that'd be a very useful update to add.

I also have a question about this function: We can't use it to update 
primary keys.. I'm wondering why ? I want to also add that support to it.

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/f532b4fd-aba6-44dd-b373-e6e934ff8b67n%40googlegroups.com.

Reply via email to